C

[C] 229 문자열에 대한 임시 저장소 만들기 strdup

qkrwngus 2021. 2. 7. 17:03

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 :