op= operators

Henry Spencer henry at utzoo.UUCP
Fri Oct 5 03:33:27 AEST 1984


> I am not sure how K&R specify that
> 	*ptr++ += 2;
> should be evaluated. Page 191 says only that the behaviour of E1 op= E2
> is the same as E1 = E1 op E2, but does that mean in this case
> 	*ptr++ += *ptr++ + 2;
> 	*ptr += *ptr++ + 2;
> or
> 	*ptr++ += *ptr + 2; ? (and what does the first form mean?)
> 
> For out 4.2 bsd compiler, the last form is used, with the incrementation of
> the pointer after the addition of 2. Is this guaranteed by the standard?

If you look carefully at page 191, you'll see that it also says "however,
E1 is evaluated only once".  So there is only one ++ involved.  The right
expansion for this is:

	*ptr = *ptr + 2;
	ptr++;

Note that no expansion with only one statement is really correct, since
the order of side effects (++ and =) within a statement is pretty much
undefined in C.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry



More information about the Comp.lang.c mailing list