所以,我看到了这个C语言中的
strcpy
实现:void strcpy1(char dest[], const char source[])
{
int i = 0;
while (1)
{
dest[i] = source[i];
if (dest[i] == '\0')
{
break;
}
i++;
}
}
我觉得它甚至将源代码中的\0
复制到目标代码中。
我也见过这个版本:
// Move the assignment into the test
void strcpy2(char dest[], const char source[])
{
int i = 0;
while ((dest[i] = source[i]) != '\0')
{
i++;
}
}
对我来说,当尝试将source
中的\0
赋值给dest
时,它会中断。
正确的选项是复制\0
还是不复制?
while
循环在\0
处终止时,复制已经通过dest[i]=source[i]
完成。 - pascal'\0'
表示字符串的结尾,必须存在,否则你将不知道字符串何时结束。 - Maroun