Possible duplicate:
Undefined Behavior and Sequence Points
As defined in the standard, E1 + = E2 is almost the same for E1 = E1 + E2, except that E1 is evaluated only once. Thus, in addition, there will be "p + = (* p) ++ + c"; cause undefined behavior?
Try using the following code in gcc / g ++ (4.7 / 4.4). There are two kinds of results: bxxxxx (g ++ 4.7) or axbxxx (gcc, g ++ 4.4). If we execute (1), but not (2) in the code, we can only get axbxxx.
#include <stdio.h> int main() { char s[] = "axxxxx"; char *p = s; printf("s = %s in the beginning.\n" "p is pointed at the %d-th char.\n", s, p - s); //p = p + (*p)++ * 3 + 2 - 'a' * 3; // (1) p += (*p)++ * 3 + 2 - 'a' * 3; // (2) printf("p is moved ahead by %d steps\n", p - s); printf("s = %s after the operation.\n", s); return 0; }
I cannot find why this causes undefined behavior, and I cannot claim that this is a gcc error.
For the result of axbxxx, I also can not understand why the operand or post ++ is evaluated twice (after receiving the value and then saving it). Since the standard says that "1 ... is added to it," I think that the address should be evaluated only once. If the address of the post ++ operand is evaluated only once, the effect of the expression will be the same, despite the fact that assignments are made in any order.
=== UPDATE ===
After reading the document linked in the first comment, I think the following rule matters:
"2) In addition, the previous value should be consulted only to determine the value to be stored.".
So, will access p in "p = p + (* p) ++ * 3 + c" be considered as part of the "previous value" * p, which has nothing to do with the value that needs to be stored in * p?
IMO, this rule is not violated.