重要事项:
_mbsdup
不能在中执行的应用程序中使用Windows 运行时。
语法:
char *_strdup(
const char *strSource
);
wchar_t *_wcsdup(
const wchar_t *strSource
);
unsigned char *_mbsdup(
const unsigned char *strSource
);
_strdup
函数调用 malloc 来为 strSource
的副本分配存储空间,然后将 strSource
复制到分配的空间。所以,最后要调用free释放堆内存。
_wcsdup
和 _mbsdup
分别是 _strdup
的宽字符及多字节字符版本。 _wcsdup
的参数和返回值是宽字符字符串;而 _mbsdup
的则是多字节字符字符串。
必要的头文件
_strdup <string.h>
_wcsdup <string.h> 或 <wchar.h>
_mbsdup <mbstring.h>
示例:
#include <string.h>
#include <stdio.h>
int main( void )
{
char buffer[] = "This is the buffer text";
char *newstring;
printf( "Original: %s\n", buffer );
newstring = _strdup( buffer );
printf( "Copy: %s\n", newstring );
free( newstring );
}
输出:
Original: This is the buffer text
Copy: This is the buffer text