Recommended Posts

Given this function.....
void strcpy4(char dest[], const char source[])
{
while (*dest++ = *source++);
}
Does the increment from the ++ operator happen before the boolean test in the while or after? So when the source reaches '\0', the order is:
1. assignment of '\0' to destination
2. check for FALSE ('\0' is the equivalent of boolean false)
3. end the while without incrementing since it tests FALSE
Is this correct? It doesn't do the increment prior to the boolean check, correct?

Share this post

Link to post

Share on other sites

Original post by Palidineit does increment _after_ the check. what you have, however, isn't a boolean check it's an assignment.

boolean check is == not =

also if your arrays are different sizes, your app will crash

-me

It's a string copy function. It's actually meant to be an assignment. It takes advantage of the fact I can do a boolean check this way. It's not checking the equality of the two strings in comparison, it's copying source to destination and breaking when it hits '\0', which is logical false.