这是 strcpy 代码
char *strcpy(char *dest, const char* src)
#endif
{
char *ret = dest;
while (*dest++ = *src++)
;
return ret;
}
我尝试仅使用交换方法和指针来实现它
char *strcpy(char *dest, char *src)
{
char *ret = dest;
dest = src;
src = ret;
return ret
}
但是swap方法不起作用,谁能告诉我使用swap方法执行strcpy()函数是否正确?如果没有,我如何仅使用 ponters 表示法编写此代码?
最佳答案
can any one tell me if it's proper to use swap method to perform strcpy() function?
不,这是不正确的,因为它不是 strcpy()
所做的。我认为你最好阅读strcpy() man page再说一遍——它被命名为 strcpy()
(它是“字符串复制”的缩写)而不是 swap_char_pointers()
。
if not, how can I write this code using ponters notation only?
您发布的第一个 strcpy()
实现仅使用指针表示法,并且工作正常。
关于c - 如何仅使用指针重写这个 strcpy() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49703626/