Chinaunix首页 | 论坛 | 博客
  • 博客访问: 363753
  • 博文数量: 84
  • 博客积分: 1970
  • 博客等级: 上尉
  • 技术积分: 970
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-13 20:42
文章分类
文章存档

2011年(1)

2010年(4)

2009年(29)

2008年(50)

我的朋友

分类: C/C++

2008-09-08 11:04:50

用法:#include
 功能:复制字符串s
 
 说明:返回指向被复制的字符串的指针,所需空间由malloc()分配且可以由free()释放。
 
 举例:
 
 
      // strdup.c
      
      #include
      #include
 
      main()
      {
        char *s="this is just f";
        char *d;
        
        d=strdup(s);
        printf("%s",d);
 
        getchar();
        return 0;
      }

strdup()主要是拷贝字符串s的一个副本,由函数返回值返回,这个副本有自己的内存空间,和s不相干。

char *strdup(const char *s)
{
        char *t = NULL;
        if (s && (t = (char*)malloc(strlen(s) + 1)))
        strcpy(t, s);
        return t;
}
阅读(1965) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~