10[25]==35? Really?

Hascall John Paul john at IASTATE.EDU
Sat Mar 2 03:21:08 AEST 1991


In article <1991Feb27.223628.13900 at unislc.uucp>, ttobler at unislc.uucp (Trent
Tobler) writes:
> From article <25694 at netcom.COM>, by avery at netcom.COM (Avery Colter):
        :
        :
> 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.

       ((char *)10)[25]   and   10[(char *)25]

   are both perfectly legal, if somewhat unorthodox.  I wouldn't go quite as
far as "don't do this", but you certainly ought to think hard about it first.
One possible example (of course, we all avoid magic numbers in our code ;-) ...

#define	CSRBASE   ((char *)10)             /* Starting address of registers */
#define FOOREG    25                       /* Foo control is register #25 */

     CSRBASE[ FOOREG ] = foo_act_cmd;      /* activate foo-o-matic */

--
John Hascall                        An ill-chosen word is the fool's messenger.
Project Vincent
Iowa State University Computation Center                       john at iastate.edu
Ames, IA  50011                                                  (515) 294-9551



More information about the Comp.lang.c mailing list