功能strdup()
和strndup()
最终成为即将推出的C23标准:
7.24.6.4 The 100 function
Synopsis
#include <string.h> char *strdup(const char *s);
strdup
函数在分配的空间中创建s
指向的字符串的副本,就像调用malloc
一样.Returns
7.24.6.5 The 100 function
Synopsis
#include <string.h> char *strndup(const char *s, size_t size);
strndup
函数在分配的空间中创建一个字符串,该字符串使用s
指向的数组的不超过size
个初始字符初始化,直至第一个空字符(以先到者为准),就像调用malloc
一样.如果s
所指向的数组在前size
个字符内不包含null,则会将null附加到该数组的副本中.Returns
为什么不考虑将POSIX-2008功能strnlen
包括在内?
#include <string.h>
size_t strnlen(const char *s, size_t maxlen);
strnlen()
函数应计算数组中s
个指向的字节数(不包括终止NUL字符)或maxlen
参数的值中的较小者.strnlen()
函数不得判断s
所指向的数组的maxlen
个字节以上.