R
Richard
Bartc said:bert said:Rahul wrote:
int main()
{
int aa=0,b=0;
1>0?aa:b = 10;
printf("value %d %d\n",aa,b);
return(0);
}
and output is value 0 0
The result of the ?: operator is not an lvalue, and cannot be
assigned to.
If you want to conditionally assign to one of two objects, it's
probably clearer to write it in full:
if (1>0)
aa = 0;
else
b = 10;
If you really want unreadable code, I suppose you could do this:
int *ip[2] = {&b,&aa};
*ip[1>0] = 10;
but if I worked with you I wouldn't thank you for it
For such requirements, I have written - and
been satisfied with - code of the style:
*(i > 0 ? &aa : &b) = <complicated expression>;
Well done - you'll make maintainers very happy in years to come.
I can't decide whether that's sarcasm from you, or real praise.
I don't think it was praise..
The main thing "wrong" with your alternative
is the omission of a block structure.
{
int v = complicated expression;
if (i)
aa = v;
else
b = v;
}
would make it perfectly clear to a maintainer
in years to come that the only purpose of v
was to be assigned either to aa or to b.
There was nothing much wrong with the original syntax:
a ? b : c = x;
(maybe better as (a ? b : c) = x
except that C doesn't allow it, and it's unfortunate that the legal format
is a little messy:
So nothing wrong with it except for the fact that C doesnt allow it? Are
you trolling or just up for some sort of special award?
*(a ? &b ? &c) = x;
This is what the programmer wants to do, so why force him to create
unnecessary statements and to duplicate expressions, or declare unnecessary
temporary variables? All extra clutter.
Dont be ridiculous. Everything is a trade off between readability,
maintainability and efficiency. The above is , IMO, too clever for its
own good. And its certainly no more efficient.
Exactly why a?b:c can't appear like that on the left-hand-side of an
assignment is a bit of a mystery;
There is no mystery about it.
after all a, a.b, a->b, a and so on can
all appear on the lhs without the programmer having to insert explicit
address-of operators.
And that has to do with ?: how?