random c++ question thingy
Posted: 16 Oct 2007, 11:58
void strcpy(char *fin, char *src)
{
while (*src) {
*fin++ = *src++;
}
*fin = '\0';
}
The above code is supposed to copy one string to another; however, im wondering about two things:
1) The src incrementation will work like
*(src++) is assigned to *(fin++);
(Why?) wouldn't the first character be skipped entirely?
2) I recognize that the while loop's condition means that the loop will continue until *src points to "null"/uninit'ed data (essentially until the string ends). However, we're explicitly assign \0 (null-termination character) to the last value of *fin.
If \0 counts as null in a boolean statement then the *fin pointer will never be incremented the last time, and the last non-null char will be overwritten.
If \0 does not count as null in a boolean statement, then things might work out, but the last \0 will still be overwritten (making the \0 completely superfluous).
------------------
So I guess I'm missing something here. Anyone care to enlighten me?
{
while (*src) {
*fin++ = *src++;
}
*fin = '\0';
}
The above code is supposed to copy one string to another; however, im wondering about two things:
1) The src incrementation will work like
*(src++) is assigned to *(fin++);
(Why?) wouldn't the first character be skipped entirely?
2) I recognize that the while loop's condition means that the loop will continue until *src points to "null"/uninit'ed data (essentially until the string ends). However, we're explicitly assign \0 (null-termination character) to the last value of *fin.
If \0 counts as null in a boolean statement then the *fin pointer will never be incremented the last time, and the last non-null char will be overwritten.
If \0 does not count as null in a boolean statement, then things might work out, but the last \0 will still be overwritten (making the \0 completely superfluous).
------------------
So I guess I'm missing something here. Anyone care to enlighten me?