Here’s a quickie for your Saturday afternoon:
strcpy(s, t) /* copy t to s; pointer version 3 */
char *s, *t;
{
while(*s++ = *t++)
;
}
That’s a classic one from the K&R text (page 101). You can tell how old it is by the style of the function declaration. There’s no return type (so it defaults to int
, even though nothing is actually returned), and the types of the arguments are declared on a separate line. Although it’s far from obvious, what the function does is copy the string t
to the string s
.
Anyway, as you can see, order of operations is key. Dereferencing precedes assignment, assignment precedes evaluation, and evaluation precedes increment. Here’s what happens:
- The current position of
s
andt
are dereferenced, and the character at that position is fetched. - That character is copied from
t
tos
- The character just copied gets evaluated. The loop terminates if it’s the null character, ‘\0’.
- The pointers are incremented to the next character in the string.
- The loop repeats, copying the consecutive characters, including the null character.
It’s definitely a cool academic exercise, but it’s so obfuscated, that even the order of operations table in the K&R book is of no help. It lists ++
and *
as having the same precedence. To see what’s going on here, we need to go to the C99 spec:
The result of the postfix
++
operator is the value of the operand. After the result is obtained, the value of the operand is incremented. (page 75)
It’s definitely a bit of black magic. Use this code wisely.