void copy (char *source, char *dest) {
while (*dest++ = *source++);
}
The char that is represented by *source
is copied to the field <
char
s are integral types. Integral types are interpreted as conditionals in the following way:
0 -> false
Anything else -> true
Since "strings" in C are null-terminated (meaning 0
or '\0'
) when it reaches the end of the string it stops.
The 'result' of an assignment is the right-hand value. So x=1;
actually returns a value; in this case, '1'.
Your code copies characters until it encountered the terminating 0 at the end of the source
string.
Your interpretation of the copy is correct. The loop stops when what dest is pointing to is zero, i.e., the '\0' character. See http://en.wikipedia.org/wiki/Null-terminated_string