*p++ = *p and more

Chris Koenigsberg ckk at g.cs.cmu.edu
Thu Mar 27 03:30:40 AEST 1986


The question was "what does (*p++ = *p) do?"
The answer was that it could give one of two results, and both were legal
since the value is undefined, depending on whether the compiler decides to
increment before or after the fetch of the rvalue.

We ran into a similar problem with a section of code containing
	"i=0; while(i<N) a[i] = b[i++];"
and we found that the Sun 120 4.2 C compiler decided that a[0] gets b[1],
while the IBM RT PC ACIS 4.2 C compiler decided that a[1] gets b[1] and a[0]
gets nothing (remains null, if it was initialized this way). The error was
manifesting itself somewhere else, in a place where the char array a[]
was used, and the program behaved differently on the two machines.

So to expand the warning, you're not safe in reusing either a pointer
or an array index on both sides of an assignment statement, where one side
is unary incremented, no matter which side of the assignment the
increment operator is.

Chris Koenigsberg
ckk at g.cs.cmu.edu , or ckk%andrew at pt.cs.cmu.edu
{harvard,seismo,topaz,ucbvax}!g.cs.cmu.edu!ckk
Center for Design of Educational Computing
Carnegie-Mellon U.
Pgh, Pa. 15213



More information about the Comp.lang.c mailing list