op= construction

Dan Tilque dant at tekla.UUCP
Wed Jun 11 10:08:13 AEST 1986


>From: cgw at mruxe.UUCP (C Waldman)
>	Quick question-
>
>	In a loop, an array element is incremented, then the array index
>	is advanced to the next element. I recently coded this with
>	
>	....
>	total[i++] += f;
>	....
>	
>	After a couple of "Memory fault- core dumped" messages, I realized
>	that i is getting incremented twice. I broke it into two lines and
>	everything was ok, but I'm wondering why this construction doesn't
>	work.


Your compiler seems to have a bug in the way it evaluates total[i++].

The gospel according to K&R (p. 191):

	       The behavior of an expression of the form E1 op= E2 may be
	inferred by taking it as equivalent to E1 = E1 op (E2); however,
	E1 is evaluated only once.

Your compiler seems to be evaluating total[i++] twice, once for the left
side of the =, and once for the right.  I ran a simple test on our compiler
(cc) and it seems to work right.

==========================================================================
Dan Tilque			Googol in, Googolplex out.
(tektronix!dadla!dant)
==========================================================================



More information about the Comp.lang.c mailing list