strdup

来源:互联网 发布:男士减肥健身计划 知乎 编辑:程序博客网 时间:2024/05/17 02:26

strdup

编辑
本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧!
strdup()函数是c语言中常用的一种字符串拷贝库函数,一般和free()函数成对出现。
外文名
strdup
头文件
string.h
功 能
将串拷贝到新建的位置处
属    性
字符串拷贝库函数

目录

  1. 1 原型:
  2. 2 说明:
  3. 3 返回值:
  4. 4 Example:

原型:编辑

extern char *strdup(char *s);
头文件:string.h

说明:编辑

功 能: 将串拷贝到新建的位置处
strdup()在内部调用了malloc()为变量分配内存,不需要使用返回的字符串时,需要用free()释放相应的内存空间,否则会造成内存泄漏。

返回值:编辑

返回一个指针,指向为复制字符串分配的空间;如果分配空间失败,则返回NULL值。

Example:编辑

①.// 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("%s\n",d);
free(d);
}
getchar();
return 0;
}
运行结果:
Golden Global View
②.Example:
CString sPath="d:\\1.jpg";
LPTSTR str = strdup( sPath );
0 0
原创粉丝点击