strdup的用法

       原型:extern char *strdup(char *s);
  頭文件:#include <string.h>
  用法:char *strdup(char *s);
  功能:複製字符串s
  說明:strdup()在內部調用了malloc()爲變量分配內存,當程序結束後,必須用free()釋放相應的內存空間,否則會造成內存泄漏
  舉例:
  // strdup.c
  //#include <syslib.h>
  #include <stdio.h>
  #include <string.h>
  int main()
  {
  char *s="Golden Global View";
  char *d;
  clrscr();
  d=strdup(s);
  printf("%s",d);
  free(d);
  getchar();
  return 0;
  }
  例
  CString sPath="d:\\1.jpg";
  LPTSTR str = strdup( sPath );
發佈了15 篇原創文章 · 獲贊 15 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章