Another silly question

Tim Olson tim at crackle.amd.com
Thu May 18 11:47:20 AEST 1989


In article <2336 at Portia.Stanford.EDU> mesmo at Portia.Stanford.EDU (Chris Johnson) writes:
| 	The supposed proof of a[i] == i[a] rests on the faulty
| 	assumption that (x+y) == (y+x) in all contexts; this is
| 	not correct.
| 
| 	When "+" denotes simple (ie int/float/etc) arithmetic, the
| 	operation commutes; when it denotes pointer arithmetic,
| 	commutation is not legal/meaningful.
| 
| 	The statement that *(a+i) == *(i+a) is therefore invalid.

Why do you think that commutation is not legal for pointer arithmetic?
It certainly is still associative:

	(pointer + 3) +5 	<==>	pointer + (3 + 5)

K&R simply say that the "+" operator (as well as "*", "&", "|", and "^")
is commutative and associative, without mentioning any restrictions.

The (d)PANS says, in the constraint section for additive operators that
"For addition, either both operands shall have arithmetic type, or one
operand shall be a pointer to an object type and the other shall have
integral type."

It doesn't say that "... or the first operator shall be a pointer...",
which certainly seems to mean that pointer addition is commutative.


	-- Tim Olson
	Advanced Micro Devices
	(tim at amd.com)



More information about the Comp.lang.c mailing list