strcpy函数为什么要返回指针?

本文探讨了strcpy函数返回指针的原因,除了支持链式表达式的观点外,还提出了当源字符串与目标字符串重叠时,返回指针能够确保正确性的论点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经常在面试时被问道的一个题目:strcpy函数为什么要返回指针?

看了好多人的说法,个人认为只有林锐的是正确的。答案:为了实现链式表达式。(林锐的答案,没错,是这样的,我同意)
我想增加另外一个原因或者解释:即strcpy函数能正常运行有如下要求:
Return Value
Each of these functions returns the destination string. No return value is reserved to indicate an error.
The strcpy function copies strSource, including the terminating null character, to the location specified by strDestination. No overflow checking is performed when strings are copied or appended. The behavior of strcpy is undefined if the source and destination strings overlap.
所以,当strDestination和strSource两者出现交叠时strDestination不能正确返回值,所以此时可以考虑新增char指针来返回原本正确的值。所以strcpy函数一定要返回指针!

转自 http://2wdragon.blog.sohu.com/95488072.html
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值