A pointer to a 3-D array, or an array of ptrs to 2-D arrays?

Chris Torek chris at mimsy.umd.edu
Mon Nov 20 03:38:13 AEST 1989


In article <1989Nov15.145302.3906 at virtech.uucp> cpcahil at virtech.uucp
(Conor P. Cahill) writes:
>if p is declared as
>	int  (*p) [13][2][64];
>then p is a pointer to an array[13][2][4] of ints.

More precisely, p is a pointer that can point to zero or more contiguous
such objects, and:

>... to place a value into position i,j,k you need to:
>	(*p)[i][j][k] = 6;
>which says place a 6 into element i,j,k of the array to which p points.

this puts a 6 into the i/j/k element of the first (0th) array to which
p points.

>	p[i][j][k] = 6;
>says place a 6 into the i,j,k'th occurance of such an array starting
>at the location p.

I think this is confusing.  p[i] names the i'th object to which p points;
this is an `array 13,2,4 of int'; then the [j] names the j'th object to
which the pointer one gets by The Famous Rule points, and this is an
`array 2,4 of int'; the [k] then names the k'th object to which the
pointer one gets by The Rule points, which is an `array 4 of int'.  This
is not a modifiable object, so the assignment is not legal.

>	*p[i][j][k] = 6;
>says place a 6 into the location pointed to by the data at the i,j,k'th
>occurance of such an array starting at location p.

Since the binding is *(p[i][j][k]), the resolution of p[i][j][k] is as
above, but the result is in a value context, not an object context.  It
therefore becomes a pointer (by The Rule) and *(the-pointer-we-just-got)
names the 0'th element of that k'th array of that j'th array of that
i'th array of objects-to-which-p-points.  This *is* a modifiable object
(assuming it has not been declared `const') and the assignment is
legal, providing that p really points to at least i contiguous objects,
and providing that j and k are in bounds.

>Also note that *p[12][1][3] caused a core dump since it is way beyond
>the end of address space for this program.

Right: p only points to one object, and p[12] asks for the thirteenth
(p[0] being the first).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list