I came across this scenario more than a year back, and again today the same scenario is back, but no answers yet, and hence posting the same thoughts again :)
While writing a piece of software I got this thought about a feature that could be provided in programming languages (at least I am not aware of any programming language which has this feature!)
Usually when we want to assign a value of a variable a to another variable say b , then all we do is write somthing like this
Now consider this case. Suppose we want to assign the value of variable a to b if c equals 1 OR else we want to assign d to b , then we write
an easier way to write the above code would be to use a ternary operator as
The Problem: Ternary Denial!
Now consider another scenario, where we want to assign a to b if c equals 1 OR we want to assign a to e
Oops then we cant use the ternary operator, instead we have to fallback to our old friend if.. else as
The Solution: Reverse Assignment Operator
Now wouldnt it be nice if this could be achieved using something like
where # is the reverse assignment operator which assigns LHS to RHS !
so here if c==1 then # would assign a to b else it would assign a to e !!!
Just some crazy thoughts while writing some crazy code :)
You can use a pointer :
d = c==1? &b : &d;
*d = a
Seems old comments have been missed here ??
This one is a recycled post sainath. Guess you are talking about the comments in the original post linked below :)
I have a trick except it takes an additional variable.
int dummy = c==1?b=a:(e=a);