|
|
On 30 Mar, 19:11, Richard <de...@xxxxxxxxx> wrote:
> bert <bert.hutchi...@xxxxxxxxxxxxxx> writes:
> > On 30 Mar, 15:02, Philip Potter <p...@xxxxxxxxxxxx> wrote:
> >> Rahul wrote:
> >> > Hi Everyone,
>
> >> > I have the following piece of code, and i expected an error, however
> >> > i don't get an error,
>
> >> > 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
>
> >> > Thanks in advance ! ! !
>
> >> 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.
> What on earth is wrong with
>
> v= complex expr;
> if(i)
> aa=v;
> else
> b=v;
>
> Same number of characters give or take too .... Easy to see
> flow/assignment in a debugger too. Very easy to read.- Hide quoted text -
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.
--
|
|