C语言strdup()函数:复制字符串【转】

本文转载自:http://c.biancheng.net/cpp/html/166.html

头文件:#include <string.h>

定义函数:char * strdup(const char *s);

函数说明:strdup()会先用maolloc()配置与参数s 字符串相同的空间大小,然后将参数s 字符串的内容复制到该内存地址,然后把该地址返回。该地址最后可以利用free()来释放。

返回值:返回一字符串指针,该指针指向复制后的新字符串地址。若返回NULL 表示内存不足。

范例

  1. #include <string.h>
  2. main(){
  3. char a[] = "strdup";
  4. char *b;
  5. b = strdup(a);
  6. printf("b[]="%s" ", b);
  7. }


执行结果:
b[]="strdup"

原文地址:https://www.cnblogs.com/zzb-Dream-90Time/p/7665708.html