strcpy的VS的strdup [英] strcpy vs strdup

查看:162
本文介绍了strcpy的VS的strdup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的strcpy 是复制一个字符串,的strdup 返回一个指向一个新的字符串复制该字符串。

I read that strcpy is for copying a string, and strdup returns a pointer to a new string to duplicate the string.

能否请您解释一下什么情况下你preFER使用的strcpy 和使用什么样的情况下你preFER 的strdup

Could you please explain what cases do you prefer to use strcpy and what cases do you prefer to use strdup?

推荐答案

的strcpy(PTR2,ptr1的)等同于而(* PTR2 ++ = * ptr1的++)

在这里为的strdup相当于

where as strdup is equivalent to

ptr2 = malloc(strlen(ptr1)+1);
strcpy(ptr2,ptr1);

所以,如果你想,你已经复制在其他功能使用(因为它是在堆一节中创建),可以使用的strdup字符串,否则strcpy的就够了。

So if you want the string which you have copied to be used in another function (as it is created in heap section) you can use strdup, else strcpy is enough.

这篇关于strcpy的VS的strdup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆