Mail Archives: djgpp/1998/06/29/10:30:23
On 25 Jun 1998 18:41:06 GMT, "Brett Kugler" <w003132 AT mail DOT airmail DOT net>
wrote:
>Ok, I don't usually post here, but I've run out of options. I've been
>trying to figure out why I'm getting an error on this line of code:
> int *test = new int[8][8];
>The error I get is:
> main.cpp:15: initialization to `int *' from `int (*)[8]'
>According to my C++ book (How To Program C++ by Deitel), that is a
>perfectly valid statement.
Then your C++ book is in error.
>I only have a vague notion of what the compiler
>is trying to tell me.
It is telling you that the type of 'test' and the type of 'new
int[8][8]' are not compatible.
> I have tried:
> int *test = new int[8];
>and this works, so it's something to do with the extra dimension in the
>array.
Yes. The expression 'new int [n1] [n2]' allocates an array of n1
objects of type int[n2]. It returns a "pointer to array of n2 ints".
This is not a "pointer to int".
The pointer the expression is assigned to has to be declared as
int (*p)[n2];
n2 beeing a _constant_ if you want to access the allocated memory
through 'test' using the [][] syntax. Only n1 may be "variable".
Regards
Horst
- Raw text -