String copy idiom.

Rob Warnock rpw3 at redwood.UUCP
Wed Mar 13 10:46:57 AEST 1985


Please note that the semantics of

	while (*s++ = *t++) ;
and
	while ((*s = *t) != '\0') {s++; t++;}

are NOT the same; therefore, the generated code CANNOT be the same!
(I noticed this while comparing the code generated on the 68000 compiler
I use.) The first statement leaves "t" pointing at the byte AFTER the null,
while the second leaves "t" pointing to the null. Auto-incrementing cannot
be used in the second case, unless your compiler generates code to "back out"
the final incrementation (an optimization I have on occasion applied by hand
to tight assembly code, but have never seen a compiler use).

The following two ARE equivalent (by the definition of "true" in boolean tests
and due to the "usual conversions" applied to '\0' before the comparison), and
the compiler I use indeed generates the same code for both cases:

	while (*s++ = *t++) ;
and
	while ((*s++ = *t++) != '\0') ;


Rob Warnock
Systems Architecture Consultant

UUCP:	{ihnp4,ucbvax!dual}!fortune!redwood!rpw3
DDD:	(415)572-2607
USPS:	510 Trinidad Lane, Foster City, CA  94404



More information about the Comp.lang.c mailing list