Pointer/array compatibility

Julian Cowley cs211s14 at uhccux.uhcc.hawaii.edu
Fri Sep 29 08:05:08 AEST 1989


I have some basic questions regarding arrays and pointers:

int a[2][3][5], ***ippp, **ipp, *ip, i, j;

i = a[0][0][0];		/* ok */
ip = &a[0][0][0];	/* ok */

ip = a[0][0];		/* why? */
ipp = a[0];		/* why? */
ippp = a;		/* why? */

i = ipp[i][j];		/* why^2? */

Why are the latter expressions legal?  For instance, I thought
that a[0][0] is interpreted as pointer to array 5 of int and ip
as a pointer to int, so they would be incompatible.  It becomes
especially confusing with the next expression: a[0] is a pointer
to an array 3 of array 5 of int, not a pointer to pointer to
int.

Also, ipp[i][j], as indicated in K&R, becomes *(*(ipp + i) + j).
How does the compiler know what size to multiply i by?  It would
seem to me that by the rules of pointer arithmetic, i would be
multiplied by the size of a pointer to int and would give
incorrect results.

Basically what I'm asking here is how the compiler decides that
two pointer expressions are of the same type and how it goes
about deciding the size of a pointed-to object.

Thanks for your help.

Julian Cowley
cs211s14 at uhccux.uhcc.hawaii.edu
cs211s14 at uhccux.bitnet



More information about the Comp.lang.c mailing list