Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!amdcad!crackle!tim From: tim@crackle.amd.com (Tim Olson) Newsgroups: comp.lang.c Subject: Re: Another silly question Message-ID: <25671@amdcad.AMD.COM> Date: 18 May 89 01:47:20 GMT References: <17812@cup.portal.com> <2336@Portia.Stanford.EDU> Sender: news@amdcad.AMD.COM Reply-To: tim@amd.com (Tim Olson) Organization: Advanced Micro Devices, Inc. Sunnyvale CA Lines: 31 Summary: Expires: Sender: Followup-To: In article <2336@Portia.Stanford.EDU> mesmo@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@amd.com)