[net.lang.c] op= operators

rhl@astrovax.UUCP (Robert the Good) (10/03/84)

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?
			Robert

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (10/03/84)

	*ptr++ += 2;
means:
	get the datum addressed by ptr;
		(increment ptr as a side-effect, AFTER getting the datum)
	add 2 to the datum;
	put the modified datum back from whence it came;
		(if any action is needed to fix side-effects, do it now)
This is figured out by following the operator precedence hierarchy.

henry@utzoo.UUCP (Henry Spencer) (10/04/84)

> 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

eich@uiucdcsb.UUCP (10/05/84)

Page 191 also says that in

	E1 op= E2

E1 is evaluated only once.  Page 187 discusses evalution of postfix ++:

	When postfix ++ is applied to an lvalue the result is the
	value of the object referred to by the lvalue.  After the
	result is noted, the object is incremented in the same manner
	as for the prefix ++ operator.

so the reference manual is quite clear: taken together, these
guarantees assure the expected interpretation.

Brendan Eich
uiucdcs!eich