头文件:#include <string.h>
定义函数:char * strdup(const char *s);
定义函数:char * strdup(const char *s);
使用man来看一下:
The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained
with malloc(3), and can be freed with free(3).
The strndup() function is similar, but copies at most n bytes. If s is longer than n, only n bytes are copied, and a terminating
null byte ('\0') is added.
返回值:返回一字符串指针,该指针指向复制后的新字符串地址。若返回NULL 表示内存不足。
这个函数的应用场景一般是在保存永久信息,也就是说一般不会使用free来释放,比如说公共的配置信息。