So, I have seen this strcpy
implementation in C:
void strcpy1(char dest[], const char source[])
{
int i = 0;
while (1)
{
dest[i] = source[i];
if (dest[i] == '\0')
{
break;
}
i++;
}
}
Which to me, it even copies the \0
from source to destination.
And I have also seen this version:
// Move the assignment into the test
void strcpy2(char dest[], const char source[])
{
int i = 0;
while ((dest[i] = source[i]) != '\0')
{
i++;
}
}
Which to me, it will break when trying to assign \0
from source
to dest
.
What would be the correct option, copying \0
or not?
while
breaks on the\0
, the copy has already been done bydest[i] = source[i]
. – Appeasement'\0'
indicates the end of string, it should be there, otherwise you'll not know where your string ends. – Rayner