c - 如何仅使用指针重写这个 strcpy() 函数?

标签 c

这是 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/

相关文章:

你能以与字节顺序无关的方式获得指向低字节的指针吗?

c - C 中的引用计数和函数内部分配的内存

c - 使用另一个过程而不是直接使用 main()

c++ - C/C++ 套接字 : IPv6 TCP connection fails if I do anything before it

c - 如何使用 blas 以最佳方式转置矩阵?

c - fgetc() 和/或 fputc() 不一致的原因

c - 第二次使用指针读取结构会出现段错误

有人可以帮助我了解如何在 c 中正确分配吗?

c - 我的 pop 函数(队列)C 做错了什么

将不带引号的斜杠转换为换行符,无需 strtok 或进一步分配内存