Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Two-dimensional dynamic array

21 views
Skip to first unread message

Chris Ahlers

unread,
Jan 28, 2001, 1:48:25 PM1/28/01
to
I am having a problem with the following code. I always receive a
compilation error.

int x = 5;
int y = 10;

char* name;
name = new char[x][y];

The same thing happens when I use:

name = new char[x][10];

or some other combination of a constant and a variable.

The only way I can get it to compile is if I create a one-dimensional
array. This does not help me.

Any help is appreciated...

--
--------------------------------------------------------------
Chris Ahlers
chris...@yahoo.com
ICQ: 1822282

Sean Kelly

unread,
Jan 28, 2001, 3:20:35 PM1/28/01
to
"Chris Ahlers" <chris...@yahoo.com> wrote in message
news:9037816A2chris...@146.186.15.11...

> I am having a problem with the following code. I always receive a
> compilation error.
>
> int x = 5;
> int y = 10;
>
> char* name;
> name = new char[x][y];

char** name;
name = new char[10];
for( int i = 0; i < 10; i++ )
name[i] = new char[10];


Sean


Chris Ahlers

unread,
Jan 28, 2001, 5:16:39 PM1/28/01
to
Makes perfect sense. Thank you.


ken...@pacbell.net (Sean Kelly) wrote in <S8%c6.77730$Wq1.33045472@nnrp5-
w.sbc.net>:

Sahan Amarasekera

unread,
Jan 28, 2001, 5:20:08 PM1/28/01
to
On Sun, 28 Jan 2001 12:20:35 -0800, "Sean Kelly" <ken...@pacbell.net>
wrote:

OK, but doing it this way makes it tedious to deal with exceptions.
You haven't even written in the necessary code, probably because you
can't be bothered :-)

Why not avoid this screwing around and just use a vector of strings ?
??

----
Sahan Amarasekera

to email me, remove animal in email address:
sahan...@amarasekera.freeserve.co.uk

>Sean
>

Sahan Amarasekera

unread,
Jan 28, 2001, 5:20:39 PM1/28/01
to
On 28 Jan 2001 22:16:39 GMT, chris...@yahoo.com (Chris Ahlers)
wrote:

>Makes perfect sense. Thank you.

I would advise you to use a vector of strings.

----
Sahan Amarasekera

to email me, remove animal in email address:
sahan...@amarasekera.freeserve.co.uk

>
>

Donovan Rebbechi

unread,
Jan 28, 2001, 3:31:40 PM1/28/01
to
On 28 Jan 2001 18:48:25 GMT, Chris Ahlers wrote:
>I am having a problem with the following code. I always receive a
>compilation error.
>
>int x = 5;
>int y = 10;
>
>char* name;
>name = new char[x][y];
>
>The same thing happens when I use:
>
>name = new char[x][10];
>
>or some other combination of a constant and a variable.
>
>The only way I can get it to compile is if I create a one-dimensional
>array. This does not help me.

First, why not use std::string, and save yourself the hassle ?

Regardless, it's still a valid question. One could always use an extra
line of code:

name = new char*[y];
for ( int i = 0; i<y; ++i ) name[i]=new char[x];

--
Donovan Rebbechi * http://pegasus.rutgers.edu/~elflord/ *
elflord at panix dot com

Sexanity

unread,
Jan 29, 2001, 2:09:18 AM1/29/01
to
the reason you have errors is because your x and y are not const. try this

const int x;
const int y;

then do the rest using x an y.

Daniel Longest

unread,
Jan 29, 2001, 11:21:03 AM1/29/01
to

That is incorrect, the OP asked about allocating 2-dimensional arrays using
new. x and y do not need to be const for that case. For example, the
following is fine:
int size=10;
int* array=new int[size];
What you said only stands up if you're doing statically allocated arrays
int array[size]; // will not compile, size must be constant

Daniel
--
--------------------------------------------------
Daniel Longest
da...@vt.edu

"Java isn't platform-independent; it is a platform"- Stroustrup
comp.lang.c++ http://www.parashift.com/cpp-faq-lite/
alt.comp.lang.learn.c-c++ http://www.faqs.org/faqs/C-faq/learn/
"People who are ignorant of their ignorance are dangerous" - Cline

"Sexanity" <sexa...@aol.com> wrote in message
news:20010129020918...@ng-ck1.aol.com...

Davlet Panech

unread,
Jan 29, 2001, 11:18:03 AM1/29/01
to
In article <9037816A2chris...@146.186.15.11>,

chris...@yahoo.com (Chris Ahlers) wrote:
> I am having a problem with the following code. I always receive a
> compilation error.
>
> int x = 5;
> int y = 10;
>
> char* name;
> name = new char[x][y];

Other posters have suggested the following:

char **name=new char *[x];
for(int i=0; i<x; i++) name[i]=new char[y];

However, the above solution creates a 1D array of pointers to 1D arrays
of chars. This is _not_ a 2D array of chars in the same sense as a
compile-time definition:

char name[5][10];

There is a way to accomplish the same thing with `new' operator,
provided that the second dimension of your array (y) is constant:

typedef char array_t[10];
array_t *name=new array_t[x];

or, in one line:

char (*name2)[10]=(char(*)[10])new char[x*10];


HTH,
D.P.


>
> The same thing happens when I use:
>
> name = new char[x][10];
>
> or some other combination of a constant and a variable.
>
> The only way I can get it to compile is if I create a one-dimensional
> array. This does not help me.
>
> Any help is appreciated...
>
> --
> --------------------------------------------------------------
> Chris Ahlers
> chris...@yahoo.com
> ICQ: 1822282
>


Sent via Deja.com
http://www.deja.com/

gba...@my-deja.com

unread,
Jan 29, 2001, 1:44:07 PM1/29/01
to
In article <95453g$vcf$1...@nnrp1.deja.com>,
Davlet Panech <davlet...@hotmail.com> wrote:
> In article <9037816A2chris...@146.186.15.11>,
[snip]

> Other posters have suggested the following:
>
> char **name=new char *[x];
> for(int i=0; i<x; i++) name[i]=new char[y];
>
> However, the above solution creates a 1D array of pointers to 1D
> arrays of chars. This is _not_ a 2D array of chars in the same sense
> as a compile-time definition:
>
> char name[5][10];
>
> There is a way to accomplish the same thing with `new' operator,
> provided that the second dimension of your array (y) is constant:
>
> typedef char array_t[10];
> array_t *name=new array_t[x];
>
> or, in one line:
>
> char (*name2)[10]=(char(*)[10])new char[x*10];
>
> HTH,
> D.P.

Another way is to create a one dimensional array and handle the
indexing yourself (index = row * row_size + col). This is readily
implemented in template classes that can create dynamically allocated
multi-dimensional arrays of any element type and number of
dimensions.

0 new messages