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
char** name;
name = new char[10];
for( int i = 0; i < 10; i++ )
name[i] = new char[10];
Sean
ken...@pacbell.net (Sean Kelly) wrote in <S8%c6.77730$Wq1.33045472@nnrp5-
w.sbc.net>:
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
>
>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
>
>
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
const int x;
const int y;
then do the rest using x an y.
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...
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/
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.