s
가 가리키는 문자열을 복사하고, 복사된 문자열에 대한 포인터를 반환char *strdup(const char *s);
s
: 복사할 문자열string
을 복사한 메모리주소(복사된 문자열의 주소를 가리키는 포인터)NULL
#include "libft.h"
char *ft_strdup(const char *s)
{
int i;
int len;
char *new_s;
i = 0;
len = 0;
while (s[len])
len++;
new_s = (char *)malloc(sizeof(char) * (len + 1));
if (!(new_s))
return (NULL);
while (s[i])
{
new_s[i] = s[i];
i++;
}
new_s[i] = '\0';
return (new_s);
}