Another silly question

Byron A Jeff byron at pyr.gatech.EDU
Fri May 19 23:31:21 AEST 1989


In article <2336 at Portia.Stanford.EDU> mesmo at Portia.Stanford.EDU (Chris Johnson) writes:
-From article <17812 at cup.portal.com>, by Tim_CDC_Roberts at 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 at 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 at pyr.gatech.edu  uucp:	...!gatech!pyr!byron



More information about the Comp.lang.c mailing list