经常在面试时被问道的一个题目: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函数一定要返回指针!