qn on comma operator

1
2
3
4
	int a[2] = {2};
	cout << a[0] << endl;
	cout << a[1] << endl;
	cout << ( a[1] *= ( a[1] = 4, a[1] *= a[0] ) ) << endl;


why does this code give me 32 instead of 64?

shouldn't the flow of control be this way:
1)assign 4 to a[1]
2)assign 4*2=8 to a[1]
3)assign 8*8=64 to a[1]
?
This is undefined behavior because there is no sequence point between
the left-hand side and the right-hand side of the expression a[1] *= ....;

There is a sequence point between a[1] = 4 and a[1] *= a[0], meaning
that a[1] will contain 8 after both expressions are evaluated. The
value of a comma-separated list of expressions in this case is the value
of the right-hand most expression, which is 8.

Therefore the line will multiply a[1] by 8, but it is not defined at what
point a[1] is "read" as the first term in the product. It could be before,
after, or even during evaluation of the right-hand side.
Topic archived. No new replies allowed.