delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/25/18:46:32

Message-ID: <3592CD96.78DB0FBC@ipass.net>
From: Terry <iceman AT ipass DOT net>
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Dynamic Arrays and C++
References: <AEA709B50D3807D4 DOT DBB9E9D19B2BCC95 DOT 9840599F888D1195 AT library-proxy DOT airnews DOT net>
Lines: 32
Date: Thu, 25 Jun 1998 22:27:35 GMT
NNTP-Posting-Host: ts2-124-ppp.ipass.net
NNTP-Posting-Date: Thu, 25 Jun 1998 18:27:35 EDT
Organization: iPass.Net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Brett Kugler 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.  I only have a vague notion of what the compiler
> is trying to tell me.  I have tried:
> 
>    int *test = new int[8];
> 
> and this works, so it's something to do with the extra dimension in the
> array.  The reason I'm posting here is because I'm trying to compile with
> DJGPP and the book I have indicates that this should work, but it doesn't.
> 

It must be a typo in your book (or it's just a bad book).  The following
is one way to do it:

   int **test;
   test = new int *[8];
   for (int i=0;i<8;i++)
       test[i] = new int[8];

Hope this helps.

Terry

- Raw text -


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