I stumbled across this code recently:
void strcat( char* dest, char* src )
{
while (*dest) dest++;
while (*dest++ = *src++);
}
Where it looks like the *dest++ = *src++ operation is being used as a condition for the while loop. How is this assignment operation converted to boolean? I’m having a hard time understanding it.
Furthermore, is the same syntax valid in C++?
In C, a non-zero value in a logical statement counts as a true, zero as false.
And the result of any assignment statement is the value of the left operand after the assignment.
And so in the second loop, if the value assigned is
0, the result of the condition is false. In the first loop, if the value of the pointed-to variable itself is0, the condition is false.This syntax is also valid in C++.