等价于c,int main和一些解释中的strcpy函数

时间:2018-11-28 18:13:51

标签: 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()中查找两个字符串?

1 个答案:

答案 0 :(得分:2)

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

建议:

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 );
}