use of if (!cptr) and if (cptr) && a programming error

Chris Torek chris at mimsy.UUCP
Sat Jul 22 06:50:46 AEST 1989


In article <2990 at nmtsun.nmt.edu> dwho at nmtsun.nmt.edu (David Olix) writes:
>>        while ( ( myptr = my_func() )->x != myptr->y )

>>THAT WILL NOT WORK! [...]

Or, more precisely, is not guaranteed.

>Actually, "Mastering C" by Craig Bolon, SYBEX Inc., p. 273 says that
>parenthesis operators have the HIGHEST priority,

If by `priority' you mean (or Bolon means) `precedence', this is true,
but:

>therefore what's inside (myptr = my_func()) would get evaluated first.

this conclusion is unwarranted.

>Also, it specifies that grouping for the '!=' operator is from left to
>right.

This is again correct.  Grouping and precedence are matters of parsing;
that is, in the absence of parentheses,

	a != b != c


is parsed the same way as

	(a != b) != c

Parentheses, having extreme prej, er, precendence, can alter the grouping.
But grouping is not related to order of evaluation.  This should be
obvious if you consider that

	while (a == b)

has no grouping at all, but certainly does have *some* order of evaluation.
Some languages (e.g., FORTRAN) make various constraints on evaluation
order based on specific grouping constructs (e.g., parentheses), but
traditionally, C has not done so.  The pANS makes some limited constraints,
but not enough to warrant the conclusion quoted above.

In short, whether `myptr->y' is obtained before or after `(fncall)->x'
is not defined in C, and different implementations do in fact have
different evaluation order.  Koenig's _C_Traps_and_Pitfalls_ is again
correct.
-- 
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