Linux系统调用函数strdup

版权声明:欢迎转载,不必客气。 https://blog.csdn.net/CSDN_FengXingwei/article/details/83755022

Name

strdup, strndup, strdupa, strndupa - duplicate a string
复制一个字符串

Synopsis

#include <string.h>
char *strdup(const char *s);
char *strndup(const char *s, size_t n);
char *strdupa(const char *s);
char *strndupa(const char *s, size_t n);

Description

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 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.

strdupa() and strndupa() are similar, but use alloca(3) to allocate the buffer. They are only available when using the GNU GCC suite, and suffer from the same limitations described in alloca(3).

strdup()函数返回一个指向新字符串的指针,该字符串是字符串s的副本。 使用malloc(3)获取新字符串的内存,可以使用free(3)释放。

strndup()函数类似,但只复制最多n个字节。 如果s大于n,则仅复制n个字节,并添加终止空字节(’\ 0’)。

strdupa()和strndupa()类似,但使用alloca(3)来分配缓冲区。 它们仅在使用GNU GCC套件时可用,并且受到alloca(3)中描述的相同限制。

Return Value

The strdup() function returns a pointer to the duplicated string, or NULL if insufficient memory was available.
strdup()函数返回指向重复字符串的指针,如果内存不足,则返回NULL。

Errors

ENOMEM

Insufficient memory available to allocate duplicate string.
内存不足,无法分配重复的字符串。

猜你喜欢

转载自blog.csdn.net/CSDN_FengXingwei/article/details/83755022
今日推荐