-
1 static RD_INLINE RD_UNUSED char *rd_strdup(const char *s) { 2 #ifndef _MSC_VER 3 char *n = strdup(s); 4 #else 5 char *n = _strdup(s); 6 #endif 7 assert(n); 8 return n; 9 }
1 test_topics_sh = rd_strdup(val);
strdup()函數是c語言中常用的一種字符串拷貝庫函數,一般和free()函數成對出現。
- 外文名
- strdup
- 頭文件
- string.h
- 功 能
- 將串拷貝到新建的位置處
- 屬 性
- 字符串拷貝庫函數
原型:
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 );