Path: utzoo!attcan!uunet!sdrc!scjones
From: scjones@sdrc.UUCP (Larry Jones)
Newsgroups: comp.lang.c
Subject: Re: Defining a pointer to an array
Message-ID: <821@sdrc.UUCP>
Date: 23 Sep 89 22:46:26 GMT
References: <526@mindlink.UUCP>
Organization: Structural Dynamics Research Corp., Cincinnati
Lines: 31

In article <526@mindlink.UUCP>, a1082@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@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