c语言常使用的函数,见到一个记一个
1.strdup()
功能:克隆一个副本,具有独立的内存空间
声明:char *strdup(char *str);
原型:
char * __strdup (const char *s) { size_t len =strlen (s) + 1; void *new =malloc (len); if (new == NULL) return NULL; return (char *)memcpy (new, s, len); }
strndup()
功能:克隆一个n长度副本,具有独立的内存空间
声明:char *strdnup(char *str,int n);
原型:
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 withmalloc(3), and can be freed with free(3).
The strndup() function is similar, but only copies at most n bytes. If s is longer than n, only n bytes are copied, and a terminating null byte ('\0') is added.
如果s的长度比n大,只复制n长度,并在末尾加‘\0’
2.