using "=" in a conditional (was Funny Mistake)

Chris Torek torek at elf.ee.lbl.gov
Thu Mar 21 06:27:37 AEST 1991


In article <1991Mar20.031419.3677 at isis.cs.du.edu>
cla at isis.UUCP (Chuck Anderson) writes:
>You can do a nice string copy with this construct as well:
>   while (*dest++ = *source++);

Of course, you can also write it as:

	while ((*dest++ = *source++) != 0)	/* or != '\0' */
		/* void */;

which will leave no doubt in the reader's mind that you meant `test
the result of copying one element', and not `test one pair of elements'.
Commenting the empty statement (here, with `void', though `do nothing'
or `continue' or `skip' or ... are all fine) will also remove all doubt
as to whether it, too, was unintended.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list