Cryptic C code?
Bob Crane
bobc at tektools.UUCP
Fri Aug 9 10:22:23 AEST 1985
I was looking through the book, _The C Programming Language_,
and came across something very disturbing.
In chapter 5, pg. 101 it says:
As the final abbreviation, we again observe that a comparison
against \0 is redundant, so the function is often written as
strcpy(s, t) /* copy t to s; pointer version 3 */
char *s, *t;
{
while (*s++ = *t++)
;
}
Although this may seem cryptic at first sight, the notational
convenience is considerable, and the idiom should be mastered,
if for no other reason than that you will see it frequently in
C programs.
Yeaacch!!!!!! It was still very cryptic to me the tenth time that I read
it!!! A friend explained it to me by saying that the character in the
'while' expression is converted to an int and that the NULL character has
an ascii value of 0 so the test will exit when the NULL character is
encountered.
I have trouble believing that the above has advantages of great
speed OR readability over:
strcpy(s,t) /* copy t to s; pointer version 2 */
char *s, *t;
{
while ((*s++ = *t++) != '\0')
;
}
Does anyone out there support the author by saying that Version 3 of
'strcpy' is better than Version 2?
Bob Crane
!tektronix!tektools!bobc
(503)627-5379
More information about the Comp.lang.c
mailing list