Ajinkya wrote:
[given suitable items, which I snipped, and]
It discards the previous expression (ptr->y) ...
Actually, since the binding of "," is done very late, the previous
expression is "ptr->x = ptr->y".
and evaluates the next (suspicious) one, (++ptr++->y).
Right.
This one binds as ++((ptr++)->y), from which it should be clear
how to interpret it:
Remember that "a++", for some object a, fetches the value stored
in a, adds 1 to that, schedules the new value to be put back into
the object by the next sequence point, and produces (as the value
of the postfix-++ operator) the old value of the object. So
"ptr++" means "the value that was in ptr before it gets
incremented, although the increment may happen right away, or
not, at the whim of the compiler."
In "expr->member", the expression on the left is evaluated. It
must have type "pointer to struct" (or "pointer to union"), and
the structure (or union) in question must have the named member.
The result of such an expression is named member-object.
Finally "++b", for some object b, is much like "a++", except
that the value produced by the prefix-++ operator is the new
value that will be stored into b. This value is produced whether
or not b is actually incremented at that point; all you get is
the promise that the increment will occur by the next sequence
point.
The whole thing is generally bad form since a reader must sit down
and puzzle out the individual object-modifications, making sure
that no two are applied to the same object without an intervening
sequence point. It is much clearer if one writes:
ptr->x = ptr->y;
ptr->y++;
ptr++;
although I would not be unhappy with the shorter:
ptr->x = ptr->y++;
ptr++;
myself. (This squeezes two separate side effects -- modifying both
ptr->x and ptr->y -- into a single statement, but it seems clear
that ptr->x and ptr->y are distinct objects. Of course, "seeming
clear" and "being the case" are not always identical: perhaps
earlier someone did "#define x y", or "ptr" points to a union
containing "int x; double y;". However, the union trick would give
undefined behavior even with the three-statement version.)