Desc :
Source Code :
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void main()
{
char buff[] = "string copy1";
char *dup;
dup = strdup(buff); // 문자열 메모리 할당, 포인터값 반환
if (dup)
{
strcpy(buff, "string copy2"); // 다른 문자열 복사
puts(buff);
puts(dup); // 새로 할당된 것이기 때문에 변화 없음
free(dup); // 메모리 반환
}
}
Result :
'C' 카테고리의 다른 글
[C] 231 메모리 100MB 할당 (1) | 2021.02.07 |
---|---|
[C] 230 메모리 1MB 할당 (1) | 2021.02.07 |
[C] 228 문자열 null로 채우기 (1) | 2021.02.07 |
[C] 227 문자열 첫글자를 대문자로 변환하기 isalpha (1) | 2021.02.07 |
[C] 226 문자열을 NULL로 채우기 strset (1) | 2021.02.07 |