Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 따라하면서 배우는 C언어
- programmers
- Cleancode
- BASIC
- JavaScript
- php
- BOJ
- 정수론
- BFS
- 따라하며 배우는 C언어
- 생활코딩
- Algorithm
- Math
- dfs
- C
- String
- DP
- graph
- 인프런
- C언어
- web
- sorting
- 종만북
- Python
- Algospot
- 백준
- 따배씨
- udemy
- greedy
- server
Archives
- Today
- Total
몽상실현개발주의
[따배씨] 16.16 memcpy() 와 memmove() 본문
따배씨 - 따라하며 배우는 C언어
16강 전처리기와 라이브러리
16.16 memcpy() 와 memmove()
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // memcpy(), memmove()
#define LEN 6
void prt(int *arr, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
/*
- overlapping region
- pointer-to-void (datatype is unknown)
*/
int arr1[LEN] = { 1, 3, 5, 7, 9, 11};
int* arr2 = (int*)malloc(LEN * sizeof(int));
if (arr2 == NULL) exit(1);
for (int i = 0; i < LEN; i++)
arr2[i] = arr1[i];
memcpy(arr2, arr1, sizeof(int) * LEN);
prt(arr2, LEN);
memmove(arr1, &arr1[2], sizeof(int) * 4);
/*
{1, 3, 5, 7, 9, 11}
{5, 7, 9, 11, 9, 11}
*/
// 겹치는 메모리 주소가 있으면, memcopy() 는 동작하지 않음
// memmove()는 복사할 내용을 따로 저장하여 사용, memcpy()는 바로 복사
prt(arr1, LEN);
return 0;
}
- memcpy() 와 mcmmove() 는 string 뿐만 아니라 일반적인 메모리 복사에 사용
- 다른 string method 와 달리 문자열 끝의 null character 조건에 맞추지 않고 메모리에 대해서만 동작
- 배열 뿐만 아니라 동적 할당 메모리를 배열처럼 사용 할 때에도 사용 가능
- memcpy()
- 겹치는 메모리 주소가 있으면, memcopy() 는 동작하지 않음
- memmove()
- memmove()는 복사할 내용을 따로 저장하여 사용, memcpy()는 바로 복사
이 글의 모든 사진과 내용의 출처는 홍정모 교수님께 있음을 알려드립니다.
http://blog.naver.com/atelierjpro
실리콘 밸리의 프로그래머 : 네이버 블로그
안녕하세요! 홍정모 블로그에 오신 것을 환영합니다. 주로 프로그래밍 관련 메모 용도로 사용합니다. 강의 수강하시는 분들은 홍정모 연구소 카페로 오세요.
blog.naver.com
http://www.inflearn.com/course/following-c
홍정모의 따라하며 배우는 C언어 - 인프런 | 강의
'따배씨++'의 성원에 힘입어 새롭게 개발된 C 언어로 시작하는 프로그래밍 입문 강의입니다. '따배씨'와 함께 프로그래밍 인생을 업그레이드 해보세요., 따라하며 배우는 C언어 '따배씨++'의 성원
www.inflearn.com
'Language > C' 카테고리의 다른 글
[따배씨] 16.17 가변 인수 Variable Arguments (0) | 2021.11.02 |
---|---|
[따배씨] 16.15 assert 라이브러리 (0) | 2021.10.19 |
[따배씨] 16.14 표준 유틸리티 Utilities 라이브러리 (0) | 2021.10.19 |
[따배씨] 16.13 표준 수학 라이브러리 (0) | 2021.10.04 |
[따배씨] 16.12 라이브러리 (0) | 2021.10.04 |
Comments