Another silly question

Chris Torek chris at mimsy.UUCP
Thu May 18 15:04:53 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 latter assertion is exactly backwards.  Pointer arithmetic (in
the forms pointer+integer and integer+pointer) is guaranteed to be
commutative, while scalar addition is not: scalar addition of certain
values is not commutative on certain peculiar architectures---things
like negative zero or peculiar floating point values, for instance.

>The statement that *(a+i) == *(i+a) is therefore invalid.

Since pointer arithmetic is commutative, the above statement is wrong,
and *(a+i) is equivalent to *(i+a), so that a[i] and i[a] denote the
same object.  See K&R (either edition) chapter 5.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list