Linux C function() 參考手冊:strdup(複製字串)

Linux C function() 參考手冊:strdup(複製字串)

Linux C function() 參考手冊:strdup(複製字串)

 

資料來源:http://people.cs.nctu.edu.tw/~yslin/library/linuxc/main.htm
線上執行:http://www.tutorialspoint.com/compile_c_online.php
code2html:http://tohtml.com/

 

相關函數
calloc,malloc,realloc,free
表頭文件
#include<string.h>
定義函數
char * strdup( const char *s);
函數說明
strdup()會先用maolloc()配置與參數s字符串相同的空間大小,然後將參數s字符串的內容複製到該內存地址,然後把該地址返回。該地址最後可以利用free()來釋放。
返回值
返回一字符串指針,該指針指向複製後的新字符串地址。若返回NULL表示內存不足。
範例

 

#include <string.h>
#include <stdio.h>
int main()
{
char a[]="strdup";
char *b;
b=strdup(a);
printf("b[ ]=\"%s\"\n",b);
return 0;
}


 

 



發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *