c - 相当于c中的strcpy函数,int main以及一些解释

标签 c

函数 strpcy() 的等效函数是:

char *strcpy(char *d, char *s)
{
    int i = 0;
    while (s[i]) {
        d[i] = s[i];
        i++;
    }
    d[i] = '\0'; // or d[i] = 0;
    return d;
}   

为什么会结束'\0'
对于两个字符串,int main() 应该是什么样子?

最佳答案

发布的代码包含很多不必要的语句。

建议:

char *myStrcpy( char *d, char *s)
{
    char *dest = d;

    while( *d++ = *s++ );

    return dest;
} 

如何调用:

#include <stdio.h>

int main( void )
{
    char destination[1024];
    char source[] = "source string";

    myStrcpy( destination, source );

    puts( destination );
}

关于c - 相当于c中的strcpy函数,int main以及一些解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53525655/

相关文章:

c - 写数组到串口c

c - 使用外部函数反转字符串

c - ls 命令在 C 编写的程序(shell 脚本)中不起作用

用于 C 风格向下转型的 C++ 转型

c - 字符指针的行为

c - 如何识别C中整数输入的特定数字?

c - 如何在c编程中以优化的方式打印10X10行和列的V?

c - 如何在结果中找到最大数量

c - 如何调试 Apache 模块

C - ptrace 和 waitpid 的段错误