블로그 (Blog)/개발로그 (Devlogs)
char * as a reference in C
ihmin
2024. 3. 13. 14:21
void set(char **buf)
{
*buf = malloc(5*sizeof(char));
// 1. don't assign the other string, copy it to the pointer, to avoid memory leaks, using string literal etc.
// 2. you need to allocate a byte for the null terminator as well
strcpy(*buf, "test");
}
char *str;
set(&str);
puts(str);
free(str);