*p++ = *p results?

David Elins ext. 220 yde at necis.UUCP
Tue Mar 25 02:43:23 AEST 1986


In article <312 at imagen.UUCP> kevin at imagen.UUCP (Kevin L. Malloy) writes:
>In the statement *p++ = *p; what will be the result? 

My understanding, confirmed by lint, is that this is an ambiguous
statement.  (lint said "warning: p evaluation order undefined")

The value of the expression p++ is p.
Therefore the value of the expression *p++ is "the thing pointed to by p".
The side effect of postincrement is that the contents of p are incremented
AFTER the expression is evaluated.

The real ambiguity is that C does not specify in which order the sides
of an assignment operator (or any operator) get evaluated (c.f. K&R p. 49).

If the left side is evaluated first then because of the postincrementing
side effect, the value of the right side would be the next item after
the one p pointed to before the statement was executed, and the outcome
would be that p points to the next item, and that the original item and
its successor (the next item) have identical values, the value of the
original "next item".

If the right side is evaluated first when it is all over,
p will still point to the next item but now the two items will maintain
their original values.

I tried a little test program (which produced the lint message) on both
System III and System V. The two systems, as implemented here gave
different results: system V evaluated the left side first, system III
did the right side first.

Boy, this stuff is hard to write about in a clear fashion. Maybe that
is why I am a programmer and not a writer (:-).

david



More information about the Comp.lang.c mailing list