Hi,
Although the expression *((a<20)?&b:&c)=30; works but
((a<20)?b:c)=30 gives
error: invalid lvalue in assignment.
what could be the reason for it? Please need sum urgent help.
Thanks in advance.
Printable View
Hi,
Although the expression *((a<20)?&b:&c)=30; works but
((a<20)?b:c)=30 gives
error: invalid lvalue in assignment.
what could be the reason for it? Please need sum urgent help.
Thanks in advance.
The result of the left hand side of:
is a value (the value stored in either b or c). Let's say for example that b is 20 and c is 30. If a is 10 then the result of the left-hand side is 20 and you are effectively trying to say:Code:((a<20)?b:c)=30
which would of course give you that error.Code:20=30
full program:
this also gives error invalid lvalue in assignment.Code:main()
{
int a=10,b,c;
((a<20)?b:c)=30;
printf("%d",b);
}
but if i replace the line with *((a<20)?&b:&c)=30; it works.
please explain also why the second one works .
You are assigning to what the corresponding pointer points to, and that is not a problem. Where did you come across this code, anyway? It looks like a lazy hack, or rather, bad style.Quote:
Originally Posted by lazy_hack
That is off the mark. That b and c were not initialised does not matter, since the intention is to assign a value to them. What matters is that the result of the conditional operator is not an lvalue, hence the attempted assignment does not work.Quote:
Originally Posted by BEN10
I think hk_mp5kpdw's post is also wrong then.
What do you find wrong about it? hk_mp5kpdw made the point that the result of the conditional operator is not a variable that can be assigned to, but a value that cannot be assigned to.Quote:
Originally Posted by BEN10
From hk_mp5kpdw's post, let b has a value of 12345 in the code given by the OP, then this will happen
That's why the error. This is what I said in my previous post.Code:12345=30;
Fair enough: looking at your post #5 again, I see that I missed the phrase "as told to you in post#2". It would have been clearer if you had given an example there and then, or at least suggested the scenario where 20 and 30 were the "garbage" values of b and c respectively.Quote:
Originally Posted by BEN10
thanks for the explanation...
"the result of the conditional operator is not an lvalue" line is the crux i believe...
the code was from test ur c skills book....just anther intrsting book..