10[25]==35? Really?

Trent Tobler ttobler at unislc.uucp
Thu Feb 28 09:36:28 AEST 1991


>From article <25694 at netcom.COM>, by avery at netcom.COM (Avery Colter):
> With all this about the #define X1 -10, I find it interesting that
> indexing a constant yields the sum of the constant and the number inside
> the brackets.
> 
> I could RTFM till the cows come home and never would have gleaned this.

No, RTFM again.  The definition of x[y] is not (x+y); it is *(x+y).
So, 10[25] is *(10 + 25) which is illegal, since only pointers can be
dereferenced.

> 
> Silly me, I would have thought that 10[25] would yield the value
> residing in the position 25 positions away from address 10.

Not unless you do something like ((char *) 10)[25], which, depending
on how the compiler treats this, may do what you thought it would.  BTW,
don't do this.

---

   Trent Tobler  - ttobler at csulx.weber.edu



More information about the Comp.lang.c mailing list