最佳答案:
strdup() 函数是c语言中常用的一种字符串拷贝库函数,一般和 free() 函数成对出现。
详情介绍
strdup() 函数是c语言中常用的一种字符串拷贝库函数,一般和 free() 函数成对出现。

- 外文名
- strdup
- 头文件
- string.h
- 功 能
- 将字符串拷贝到新建的位置处
- 属性
- 字符串拷贝库函数
strdup原型
extern char *strdup(char *s);
头文件:string.h
strdup说明
功 能: 将字符串拷贝到新建的位置处

strdup()在内部调用了malloc()为变量分配内存,不需要使用返回的字符串时,需要用free()释放相应的内存空间,否则会造成内存泄漏。
strdup返回值
返回一个指针,指向为复制字符串分配的空间;如果分配空间失败,则返回NULL值。
strdupExample
①.

// strdup.c#include <stdlib.h>#include <stdio.h>#include <string.h>#include <malloc.h>int main(){ char *s="Golden Global View"; char *d;clrscr();d=strdup(s); if(NULL != d){ printf("%sn",d); free(d); } getchar(); return 0;}运行结果:
Golden Global View
②.Example:
CString sPath="d:1.jpg";
LPTSTR str = strdup( sPath );


