개념 :
메모리 공간원 원하는 만큼 어떤 자료형이든 복사할수 있다.
목표 :
strcpy_s 와 strcat_s를 쓰지않고 memcpy_s로 대체하기
소스코드 : main.c
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(void)
{
char *pString = "Hello JunmoZzi";
char *pAdd = " ^^a ha ha ha!";
char *pBuffer = NULL;
int nSize1 = strlen(pString) + 1;
int nSize2 = strlen(pAdd) + 1;
pBuffer = calloc(nSize1 + 1, sizeof(char));
if (pBuffer == NULL)
{
puts("memory 할당 실패");
}
else
{
puts("memory 할당 성공");
printf("메모리 주소 : %d\n", pBuffer);
//strcpy_s(pBuffer, nSize1, pString);
memcpy_s(pBuffer, nSize1, pString, nSize1);
}
puts(pBuffer);
puts("메모리 재 할당");
realloc(pBuffer, nSize1 + nSize2);
pBuffer[nSize1 - 1] = ' ';// 문자열의 끝 \0을 공백으로 변경
//strcat_s(pBuffer, nSize1+nSize2, pAdd);
memcpy_s(pBuffer + nSize1, nSize2, pAdd, nSize2);
printf("메모리 주소 : %d\n", pBuffer);
puts(pBuffer);
free(pBuffer);
pBuffer = NULL;
return 0;
}
결과
#memcpy_s, #c언어, #c언어입문, #프로그램입문
'Programing - C > C Basic grammar ' 카테고리의 다른 글
095 memmove_s (0) | 2017.07.28 |
---|---|
094 memcmp 두메모리 비교 (0) | 2017.07.28 |
092 realloc 메모리 재할당 (0) | 2017.07.28 |
091 calloc (0) | 2017.07.28 |
take rest 03 == 비교 연산자를 쓰지 않는 함수를 만들어보기 (0) | 2017.07.28 |