c语言strdup函数
In this article, we’ll take a look at using the strdup() function in C/C++.
在本文中,我们将研究在C / C ++中使用strdup()函数。
The strdup() function is very useful if you want to duplicate the contents of a string onto another string.
如果要将一个字符串的内容复制到另一个字符串上,strdup()函数将非常有用。
Let’s see how we can utilize this function, using some simple examples.
通过一些简单的示例,让我们看看如何利用此功能。
C / C ++中strdup()函数的基本语法 (Basic Syntax of the strdup() function in C/C++)
This takes in an input char*
string, and duplicates it into another char*
string. It returns the pointer to this string.
这将接受一个输入的char*
字符串,并将其复制到另一个char*
字符串中。 它返回指向该字符串的指针。
This is also defined inside the header file <string.h>
, so we must include it first.
这也是在头文件<string.h>
定义的,因此我们必须首先包含它。
Therefore, the function prototype is as follows:
因此,函数原型如下:
#include <string.h>
char* strdup(const char* src);
Since we will not be modifying the input string, we pass it as const char*
!
由于我们不会修改输入字符串,因此将其作为const char*
传递。
NOTE: This function is NOT defined in the ISO C99 standards, but it is defined in POSIX systems. If yo