Defining a pointer to an array

Larry Jones scjones at sdrc.UUCP
Sun Sep 24 08:46:26 AEST 1989


In article <526 at mindlink.UUCP>, a1082 at mindlink.UUCP (Terry Bartsch) writes:
> The construct "int y[a][b][c][d]"
> allocates a*b*c*d integers in the form of an array.
> 
> The construct "int (*x) [a][b][c][d]"
> should theoretically allocate a pointer to the same sort of array.
> 
> [ but then when you use x[a][b][c][d] (or *x[a][b][c][d], the
> article contains both), it doesn't work right. ]

Let us repeat once again, in unison, "In C, the declaration of a
variable and the use of that variable should look the same."  If
you declare "int (*x)[a][b][c][d]", then you should reference it
as "(*x)[a][b][c][d]", which works just fine, not by using either
of the methods you mentioned in your article.

However, what you probably want to do is declare x to have the
same type as y does after conversion to a pointer.  When you use
the name of an array (like "y") without a subscript, it is
converted to a pointer to the first element of the array (not a
pointer to the entire array).  Thus, "y" is a pointer to an array
of b arrays of c arrays of d integers.  If you declare x as
"int (*x)[b][c][d]", you can then use "x[a][b][c][d]" just like
you use "y[a][b][c][d]".
----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones at SDRC.UU.NET
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150-2789             AT&T: (513) 576-2070
"I have plenty of good sense.  I just choose to ignore it."
-Calvin



More information about the Comp.lang.c mailing list