所以,我strcpy在 C 中看到了这个实现:
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++;
}
}
对我来说,在尝试分配\0from sourceto时它会中断dest。
什么是正确的选择,复制\0与否?