delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/29/10:30:23

From: horst DOT kraemer AT snafu DOT de (Horst Kraemer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Dynamic Arrays and C++
Date: Mon, 29 Jun 1998 14:24:56 GMT
Organization: Unlimited Surprise Systems, Berlin
Lines: 44
Message-ID: <35979d22.56249279@news.snafu.de>
References: <AEA709B50D3807D4 DOT DBB9E9D19B2BCC95 DOT 9840599F888D1195 AT library-proxy DOT airnews DOT net>
NNTP-Posting-Host: n164-110.berlin.snafu.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019