Path: utzoo!attcan!uunet!lll-winken!xanth!ukma!gatech!gitpyr!byron From: byron@pyr.gatech.EDU (Byron A Jeff) Newsgroups: comp.lang.c Subject: Re: Another silly question Message-ID: <8277@pyr.gatech.EDU> Date: 19 May 89 13:31:21 GMT References: <17812@cup.portal.com> <2336@Portia.Stanford.EDU> Reply-To: byron@pyr.UUCP (Byron A Jeff) Organization: Georgia Institute of Technology Lines: 45 In article <2336@Portia.Stanford.EDU> mesmo@Portia.Stanford.EDU (Chris Johnson) writes: -From article <17812@cup.portal.com>, by Tim_CDC_Roberts@cup.portal.com: -> Ok, folks. In regards to "a[i] == *(a+i) == *(i+a) == i[a]", let me -> refer to the oft-used example 2["hello"]. -> I agree that this works and is equivalent to "hello"[2]. I've seen it -> in books and postings. My simple question is why? - - 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. Try this program on for size: main() { char *p = "Goofy"; printf("%c %c %d %d\n",*(p+2),*(2+p), *(p+2) == *(2+p), 2+p == p+2); } and its output: o o 1 1 Any other assertions you'd like to make? --- -============================================================================== - Chris M Johnson === mesmo@portia.stanford.edu === "Grad school sucks rocks" - "Imitation is the sincerest form of plagiarism" -- ALF -============================================================================== -- Another random extraction from the mental bit stream of... Byron A. Jeff Georgia Tech, Atlanta GA 30332 Internet: byron@pyr.gatech.edu uucp: ...!gatech!pyr!byron