srtdup与srtcpy用法总结

本文介绍了C语言中的strdup和strcpy函数。strdup函数用于创建字符串副本,返回值指向新分配的内存,需要使用free释放。strcpy则将源字符串复制到已分配内存的目的地址,使用前需确保目的指针已有足够空间。
摘要由CSDN通过智能技术生成

函数名: strdup
功 能: 将串拷贝到新建的位置处
用 法: char *strdup(char *str);

这个函数在Linux的man手册里解释为:
The strdup() function returns a pointer toa new string which is a
duplicate of the string s. Memory for thenew string is obtained with
malloc(3), and can be freed with free(3).
The strndup() function is similar, but onlycopies at most n charac-
ters. If s is longer than n, only ncharacters are copied, and a termi-
nating NUL is added.

strdup函数原型:
strdup()主要是拷贝字符串s的一个副本,由函数返回值返回,这个副本有自己的内存空间,和s不相干。strdup函数复制一个字符串,使用完后要记得删除在函数中动态申请的内存,strdup函数的参数不能为NULL,一旦为NULL,就会报段错误,因为该函数包括了strlen函数,而该函数参数不能是NULL。
strdup的工作原理:

char * __strdup (const char *s)
{
     size_t len =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值