Interview Questions

I have a complicated expression which I have to assign to one of two variables

C Interview Questions and Answers


(Continued from previous question...)

I have a complicated expression which I have to assign to one of two variables

Q: 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.

(Continued on next question...)

Other Interview Questions