[comp.lang.c] ?:

ckp@grebyn.com (Checkpoint Technologies) (02/03/90)

In article <6200014@ux1.cso.uiuc.edu> phil@ux1.cso.uiuc.edu writes:
>
>Do any compilers accept this kind of syntax (which as far as I can tell is
>bogus C since ?: does not yield an lvalue, but is not ambiguous):
>
>    ( a == b ? x : y ) += z;    /* x and y are lvalues */

	How about this:

	*((a == b) ? &x : &y) += z;

	Of course x and y must be of the same type, or at least
reasonably compatible types (if you feel adventurous - not that this is
what I'm reccommending, mind you).

squires@eecs.nwu.edu (Matt Squires) (02/08/90)

In comp.lang.c, phil@ux1.cso.uiuc.edu writes:

> Do any compilers accept this kind of syntax (which as far as I can tell is
> bogus C since ?: does not yield an lvalue, but is not ambiguous):
> 
>     ( a == b ? x : y ) += z;    /* x and y are lvalues */
> 
> as equivalent to:
> 
>     if ( a == b ) x += z; else y += z;

Sure, GNU's GCC does.  From the GCC info page:

< A conditional expression is a valid lvalue if its type is not void and the
< true and false branches are both valid lvalues.  For example, these two
< expressions are equivalent:
< 
< @example
< (a ? b : c) = 5
< (a ? b = 5 : (c = 5))
< @end example

Matt "Local GNUisance" S.
squires@eecs.nwu.edu