Possible Duplicate:
In what order does evaluation of post-increment operator happen?
Consider the following snippet(in C):
uint8_t index = 10;
uint8_t arr[20];
arr[index++] = index;
When I compile this with gcc, it sets arr[10] to 10, which means that the postfix increment isn’t being applied until after the entire assignment expression. I found this somewhat surprising, as I was expecting the increment to return the original value(10) and then increment to 11, thereby setting arr[10] to 11.
I’ve seen lots of other posts about increment operators in RValues, but not in LValue expressions.
Thanks.
Some standard language:
Paragraph 2 explicitly renders expressions of the form
a[i++] = iundefined; the prior value ofiisn’t just being read to determine the result ofi++. Thus, any result is allowed.Beyond that, you cannot rely on the side effect of the
++operator to be applied immediately after the expression is evaluated. For an expression likethe only guarantee is that the result of the expression
j++ * ++kis assigned to the result of the expressiona[i++]; however, each of the subexpressionsa[i++],j++, and++kmay be evaluated in any order, and the side effects (assigning toa[i], updatingi, updatingj, and updatingk) may be applied in any order.