Why does int a[x,y] convert into a[y], since comma operator operates left to right? I would expect a[(x,y)], since inner operation will finish first. But in the first one it is supposed to take the first argument.
I’m not planning to use the comma operator for array initialization, just asking why this happens.
I read it in a book, and I’m confused.
Update:
i = a, b, c; // stores a into i
i = (a, b, c); // stores c into i
So as first line of code says in the array the first value must be assigned to the array. Note: I’m not actually planning to use this. I’m just asking. I’m learning C++ and I read in a book that in an array declaration a[y,x]; so it should be a[y], x; not a[x]. Why does the compiler do this?
The comma operator
,is also known as the “forget” operator. It does the following:So in your case, it behaves just as it should.
a[x, y]first evaluatesx, then discards its value, then uses the value ofyas the value of the entire expression (the one in brackets).EDIT
Regarding your edit with Wikipedia. Note that the precedence of
,is less than that of=. In other words,is interpreted as
That’s why
ais copied intoi. However, the result of the entire expression will still bec.