*p++ = *p and more

Paul Gardner gardner at rochester.ARPA
Fri Mar 28 05:28:00 AEST 1986


In article <357 at g.cs.cmu.edu>, ckk at g.cs.cmu.edu (Chris Koenigsberg) writes:
> 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).

Are you sure that's the way it happens? Assuming that the "a[i] = b[i++]" is
just ambiguous and not wrong (this seems to be the consensus anyway) then
either we index into a[] first or we index into b[] and increment i first.
In the first case a[0] gets b[0] and i == 1 after the assignment. In the
second case a[1] gets b[0] and i == 1 after the assignment.
In any case the results you mention make no sense when the postfix ++ is used.
Perhaps you meant:
  	"i=0; while(i<N) a[i] = b[++i];"
It seems to fit the symptoms.

---------------
Paul C. Gardner 
UUCP:  ..!{allegra,seismo,decvax,cmcl2}!rochester!gardner



More information about the Comp.lang.c mailing list