Re: [OFFTOPIC] Re: A bit off-topic ... (fwd)

David Luyer (luyer@ucs.uwa.edu.au)
Thu, 01 Apr 1999 08:42:01 +0800


> > I understand how ?: works. What I said was that gcc/egcc would not compile
> > this line:
> > x = (y) ? x = 1 : x = 2;

Of course. you're writing a = b = c, where a is x, b is (y)?x=1:x and c is 2.
It's illegal to assign (y)?x=1:x to something - that's an obvious invalid
lvalue to me. So x = 2 but what to do with the rest, who knows.

Try this one -

main() {
int x=0, y=0, z = 0;

x ? y : z = 1;

printf("%d %d %d\n", x, y, z);
}

Compiles fine. Output: 0 0 1.
Now set x=1 and find output: 1 1 0.
Isn't that sick :-) gcc 2.7.2.3.

Based on that, a really good compiler could give your above statement as

x = (x=1) = 2

and then come out with x = 2.

> > But if you change it like this:
> > x = (y) ? (x = 1) : (x = 2);
> > then gcc/egcc don't give you an error. Try the same workaround in your own
> > compiler, or upgrade it.

Well, duh. Brackets bind more tightly than equals last time I checked.

David.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/