Evaluation order of assignment.

Chris Torek chris at mimsy.UUCP
Thu Aug 18 18:10:09 AEST 1988


>In article <13036 at mimsy.UUCP> I noted that the expression
>>>   head->next = head = <expression>;

is not guaranteed to produce the intended result; that instead, one
must use

	head = <expression>;
	head->next = head;

or, equivalently,

	head = <expression>, head->next = head;

In article <1045 at td2cad.intel.com> brister at td2cad.intel.com (James Brister)
asks:
>I'm curious; Why not?

In simplest terms: because the compiler is allowed to figure out where
`head->next' is before bothering to do the `head = <expression>' part.
In other words, you might get what was intended, but you might instead
get the equivalent of

	temp = head;
	head = <expression>;
	temp->next = head;

which is obviously quite different.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list