[comp.lang.c] 10[25]==35? Really?

avery@netcom.COM (Avery Colter) (02/25/91)

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.

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

But, I guess there must be an exception made here, because it is
effectively a void pointer being indexed, with no way of telling
how many bytes are offset for each number in the brackets?

Very strange stuff indeed.

-- 
Avery Ray Colter    {apple|claris}!netcom!avery  {decwrl|mips|sgi}!btr!elfcat
(415) 839-4567   "I feel love has got to come on and I want it:
                  Something big and lovely!"         - The B-52s, "Channel Z"

dave@cs.arizona.edu (Dave P. Schaumann) (02/25/91)

In article <25694@netcom.COM> avery@netcom.COM (Avery Colter) writes:
>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.

Well, if you would RTF code, you would've seen an initialization loop
that looked something like this:

  for( i = 0 ; i < N ; i++ ) p[i] = i ;

(That's probably not exactly what was in the code, but you get the idea.)

Just another tricky day for you...

-- 
Dave Schaumann      | Is this question undecidable?
dave@cs.arizona.edu |

brandis@inf.ethz.ch (Marc Brandis) (02/25/91)

In article <25694@netcom.COM> avery@netcom.COM (Avery Colter) writes:
>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 not find a compiler that did it like that. All of them complained
that they expected a variable to be indexed. Some of the compilers were
ANSI (IBM S/6000), some were K&R (Sun 3, Sequent Symetry).


Marc-Michael Brandis
Computer Systems Laboratory, ETH-Zentrum (Swiss Federal Institute of Technology)
CH-8092 Zurich, Switzerland
email: brandis@inf.ethz.ch

henry@zoo.toronto.edu (Henry Spencer) (02/26/91)

In article <25694@netcom.COM> avery@netcom.COM (Avery Colter) writes:
>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.

It shouldn't.  The definition of a[b] is that it acts like *(a+b) [ignoring
issues of parentheses and operator precedence].  Note the "*" on the front.
-- 
"But this *is* the simplified version   | Henry Spencer @ U of Toronto Zoology
for the general public."     -S. Harris |  henry@zoo.toronto.edu  utzoo!henry

wgh@ubbpc.UUCP (William G. Hutchison) (02/27/91)

In article <25694@netcom.COM>, avery@netcom.COM (Avery Colter) writes:
> 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.
> 
> Silly me, I would have thought that 10[25] would yield the value
> residing in the position 25 positions away from address 10.
> 
# cat ca.c
main() {
        printf("%d\n", 10[25]);
        return 0;
}
# make ca
        cc -O  ca.c -o ca
"ca.c", line 2: illegal indirection
*** Error code 1

Stop.

 I don't see how RTFMing can show you something that is not true???
-- 
Bill Hutchison, DP Consultant	psuvax1!burdvax!ubbpc!wgh (work)
Unisys UNIX Portation Center	uunet!eidolon!wgh (home)
P.O. Box 500, M.S. B121         "At the moment I feel more like arguing than
Blue Bell, PA 19424		being good" Raymond Smullyan _The Tao is Silent_

ttobler@unislc.uucp (Trent Tobler) (02/28/91)

From article <25694@netcom.COM>, by avery@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@csulx.weber.edu

john@IASTATE.EDU (Hascall John Paul) (03/02/91)

In article <1991Feb27.223628.13900@unislc.uucp>, ttobler@unislc.uucp (Trent
Tobler) writes:
> From article <25694@netcom.COM>, by avery@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@iastate.edu
Ames, IA  50011                                                  (515) 294-9551