Another (not so) silly question
David Goodenough
dg at lakart.UUCP
Sat Apr 29 00:43:26 AEST 1989
>From article <2459 at nmtsun.nmt.edu>, by kelly at nmtsun.nmt.edu (Sean Kelly):
> My CS instructor and I disagree about a certain moot point. I have a text
> book which says that
>
> *(a + i) and a[i]
>
> are equivalent, given an array a, and int index i ... each gives the
> value stored in a[i]. But he says that
>
> *(a + i)
>
> is non-standard and would not expect it do go far on all _real_ C compilers
> (_real_ meaning those compilers that are somewhat devoted to K & R or ANSI).
Your CS professor is talking out the back of his neck. This is _EXTREMELY_
standard: any compiler that can't do that is going to get in real trouble,
real fast. Ask your CS prof for his words of wisdom on:
int *b;
b++;
Then ask him about:
int *b;
b = b + 1;
> He expects that many compilers would instead add the value of i to the
> pointer a, and then reference the item stored there.
Not quite - it adds i * sizeof(*a) to a. i.e. just what happens when you do
a[i]. Get him to look at the following code segment:
int a[100], i;
i = 40;
printf("%x %x\n", &a[i], &(*(a + i)));
Now, see what it produces as output :-) In particular, look in the Gospel
according to Kernighan and Ritchie, at the section about pointer arithmetic.
--
dg at lakart.UUCP - David Goodenough +---+
IHS | +-+-+
....... !harvard!xait!lakart!dg +-+-+ |
AKA: dg%lakart.uucp at xait.xerox.com +---+
More information about the Comp.lang.c
mailing list