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.

相关文章:

  • 2022-12-23
  • 2021-09-29
  • 2022-01-21
  • 2022-12-23
  • 2021-11-07
  • 2022-12-23
  • 2021-10-29
猜你喜欢
  • 2021-09-28
  • 2021-04-01
  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
  • 2022-01-09
  • 2021-05-10
相关资源
相似解决方案