"C언어 memcpy()"의 두 판 사이의 차이

17번째 줄: 17번째 줄:
==예시==
==예시==
* int형의 배열 src배열을 dst배열로 복사
* int형의 배열 src배열을 dst배열로 복사
<source lang='C'>
<source lang='C' run>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
23번째 줄: 23번째 줄:
#define ARR_SIZE 6
#define ARR_SIZE 6


void printArray(int *arr, int n)
void printArray(int *arr, int n) {
{
     int i;
     int i;
 
     for (i = 0; i < n; i++) printf("%d ", arr[i]);
     for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
     printf("\n");
     printf("\n");
}
}


int main()
int main() {
{
     const int src[ARR_SIZE] = {0, 1, 2, 3, 4, 5};
     const int src[ARR_SIZE] = {0, 1, 2, 3, 4, 5};
     int dst[ARR_SIZE];
     int dst[ARR_SIZE];
     memcpy(dst, src, sizeof(src));
     memcpy(dst, src, sizeof(src));
     printArray(dst, ARR_SIZE);
     printArray(dst, ARR_SIZE);
     return 0;
     return 0;
}
}

2020년 2월 8일 (토) 03:34 판

1 개념

C언어 memcpy()
  • "memory copy"
  • 지정한 크기 만큼의 메모리 영역 복사

2 문법

헤더
#include <string.h>
정의
void *memcpy(void *dest, const void *src, size_t n);

3 예시

  • int형의 배열 src배열을 dst배열로 복사
#include <stdio.h>
#include <string.h>

#define ARR_SIZE 6

void printArray(int *arr, int n) {
    int i;
    for (i = 0; i < n; i++) printf("%d ", arr[i]);
    printf("\n");
}

int main() {
    const int src[ARR_SIZE] = {0, 1, 2, 3, 4, 5};
    int dst[ARR_SIZE];
    memcpy(dst, src, sizeof(src));
    printArray(dst, ARR_SIZE);
    return 0;
}

4 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}