cond. op. on ='s LHS

Karl Heuer karl at ima.isc.com
Thu Feb 14 15:15:59 AEST 1991


In article <4155 at cernvax.cern.ch> burow at cernvax.cern.ch (burkhard burow) writes:
>       *(a==b?&c:&d) = 1;

What you really want is
	(a==b?c:d) = 1;
which should (IMHO) be legal (i.e. the result of ?: is an lvalue if both
subexpressions are lvalues), but this isn't the case in Standard C.  (But
GCC supports it as a non-standard extension.)

So far everyone has complained about using ?: instead of the "clearer" method
of putting the assignment inside the conditional.  Nobody has mentioned yet
that the above form does have one advantage, which becomes more obvious when
the RHS is a complicated expression: you can write it just once without using
a temporary.

If the GCC extension were standard, I'd use it in such a case (just as I use
x=(a==b?c:d) in real C when appropriate).  But since it isn't, and making it
legal by adding the extra *...&...& is a lose, I'd probably declare a temp
variable and use (a==b?c=T:d=T) instead.  (Or the equivalent if-else form, if
it's in a statement context.)

Karl W. Z. Heuer (karl at ima.isc.com or uunet!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list