& operator

Steven M. Haflich smh at mit-eddie.UUCP
Sun Dec 18 17:02:33 AEST 1983


Regarding this fragment:

	    char *in, *out, buf[];
	    in = buf;
	    out = &(*in++);


Larry West cites K&R and notes:

		``When postfix ++ is applied to an lvalue the result is the
		  value of the object referred to by the lvalue.  After the
		  result is noted, the object is incremented...''

	Apparently, "After the result is noted" means after the statement is
	evaluated, so parentheses don't matter.   `in' keeps its old value
	until after the statement is finished.

Not quite... The term `statement' has a rather precise meaning to a
compiler.  The variable `in' is indeed modified at the same time that
the postfix ++ is evaluated, not after the entire statement.
Consider:
	char *in = "123";
	if (*in++ == *in++) ... ;	/* ought never be true */

Although except for the `,' `&&' and `||' operators C explicitly does
not specify order of evaluation within expressions (and therefore
expressions which use multiple `++' and `--' operators on the same
variable are usually crocky) I see no reason why the above conditional
could not be used to check whether the first two characters of a string
(known to be at least length two) are the same.

By the way, the comma separating function arguments is *not* the same
as the comma operator, and does *not* specify order of evaluation.
Thus, the following is implementation dependent:
	foo(*in++,*in++);

Steve Haflich
MIT Experimental Music Studio



More information about the Comp.lang.c mailing list