Question 3.16

I have a complicated expression which I have to assign to one of two variables, depending on a condition. Can I use code like this?

	((condition) ? a : b) = complicated_expression;


No. The ?: operator, like most operators, yields a value, and you can't assign to a value. (In other words, ?: does not yield an lvalue.) If you really want to, you can try something like

	*((condition) ? &a : &b) = complicated_expression;
although this is admittedly not as pretty.

References: ANSI Sec. 3.3.15 esp. footnote 50
ISO Sec. 6.3.15
H&S Sec. 7.1 pp. 179-180


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback