still problems with ?:

david.f.prosser dfp at cbnewsl.ATT.COM
Thu Jun 15 05:48:35 AEST 1989


In article <568 at lakart.UUCP> dg at lakart.UUCP (David Goodenough) writes:
]gwyn at smoke.BRL.MIL (Doug Gwyn) sez:
]> In article <4675 at alvin.mcnc.org> spl at mcnc.org.UUCP (Steve Lamont) writes:
]>>>((a = *p++) && e2 ) || (!a && e3)
]>>Is right to left evaluation mandated in this case?  It seems to me that
]>>I've been bitten by things like this before, either by non-standards
]>>conforming compilers, ambiguous definition in the standard, my own
]>>stupidity, or all of the above.  In any case, it seems that defensive
]>>programming might dictate something like ...
]> 
]> There has never been any ambiguity about the correct sequence of
]> operations during evaluation of such an expression (except when
]> `p' is used in `e2' or `e3').
]
]Huh??? - if I use p in e2 or e3, my compiler had better use the value
]_AFTER_ the *p++, otherwise I'm going to ask for my money back. In the
]above code fragment if _ANYTHING_ is executed before the a = *p++ then
]the compiler is broken. The same applies to:
]
]	if ((a = *p++) ? e2 : e3)
]
]The a = *p++ part has to be done first so that the compiler knows which
]of e2 and e3 to do. Of course if your going to tell me that the standard
]says that the a = *p and the p++ may happen at different times (i.e. the
]p++ after e2), then I'm going to say the standard is broken.
]-- 
]	dg at lakart.UUCP - David Goodenough		+---+
]						IHS	| +-+-+
]	....... !harvard!xait!lakart!dg			+-+-+ |
]AKA:	dg%lakart.uucp at xait.xerox.com		  	  +---+

David is correct:  There are sequence points at the appropriate
operators in these expressions.  Because all pending updates must
occur between the previous and the next sequence point, the update
to p in each of the following expressions must occur prior to the
evaluation of e2 or e3:

	(a = *p++) ? e2 : e3
		/* ^ sequence point */
	((a = *p++) && e2) || (!a && e3)
		/*  ^      ^      ^ sequence points */

But in
	((a = *p++) & e2) | (!a & e3)

there is no guarantee about the update of p with respect to the
evaluation of the rest of the expression.

Dave Prosser	...not an official X3J11 answer...



More information about the Comp.lang.c mailing list