multidimensional array with different row lengths

C

chy1013m1

In Java, one can do the following: int[][] array = {{1,2,1}, {1},
{0,0,0,0,0,0,0,-1}};
How can one declare such array in C++/C without using various new
operators individually to each row indicies? (with a ptr-ptr)
Also, I've noticed that it works for char[][] array:
char *charArray[] = {"123456", "1515", "1"}; compiles on mingw32
but
int *intArray[] = {{1,2,1,2,1}, {11}, {0}}; wouldn't compile.

thanks =]
 
M

Markus Schoder

In Java, one can do the following: int[][] array = {{1,2,1}, {1},
{0,0,0,0,0,0,0,-1}};
How can one declare such array in C++/C without using various new
operators individually to each row indicies? (with a ptr-ptr)
Also, I've noticed that it works for char[][] array:
char *charArray[] = {"123456", "1515", "1"}; compiles on mingw32
but
int *intArray[] = {{1,2,1,2,1}, {11}, {0}}; wouldn't compile.

The following works however:

int intArray[][5] = {{1,2,1,2,1}, {11}, {0}};
 
V

Victor Bazarov

In Java, one can do the following: int[][] array = {{1,2,1}, {1},
{0,0,0,0,0,0,0,-1}};
How can one declare such array in C++/C without using various new
operators individually to each row indicies? (with a ptr-ptr)

Not a native array, but you can do it with a vector of vectors:

vector<vector<int> > array;

And initialisation of it is a bit problematic, you'd need to use
'copy' and 'push_back'.
Also, I've noticed that it works for char[][] array:
char *charArray[] = {"123456", "1515", "1"}; compiles on mingw32

What you have is not a "char[][] array", you have a single-dimensioned
array of pointers to char.
but
int *intArray[] = {{1,2,1,2,1}, {11}, {0}}; wouldn't compile.

Right.

V
 
A

Andrey Tarasevich

In Java, one can do the following: int[][] array = {{1,2,1}, {1},
{0,0,0,0,0,0,0,-1}};
How can one declare such array in C++/C without using various new
operators individually to each row indicies? (with a ptr-ptr)
Also, I've noticed that it works for char[][] array:
char *charArray[] = {"123456", "1515", "1"}; compiles on mingw32
but
int *intArray[] = {{1,2,1,2,1}, {11}, {0}}; wouldn't compile.
...

It all depends on what it is you really need. What do you mean by "different row
lengths" and why do you need it?

If you are trying to save memory, i.e. make a sparse 2D array to consume the
amount of memory determined by the number of actually used elements, not by the
product of its maximum dimensions (m*n), then what you called 'ptr-ptr' might be
the way to go. The row arrays are not required to be allocated by 'new' though.
Your example with 'charArray' is nothing else then an implicit ptr-ptr construct
with row arrays (string literals) allocated in static memory. You can achieve
something similar with 'intArray' as well, but since there's no int-array
literals in C++, the row arrays will have to be declared explicitly:

int row1[] = { 1, 2, 1, 2, 1};
int row2[] = { 11 };
int row3[] = { 0 };

int* intArray[] = { row1, row2, row3 };

The row arrays and the 'intArray' itself can be placed in static, automatic or
dynamic ('new'-ed) memory, depending on what you need.

Of course, if you don't really care to save memory and just want to have
variable-length array initializers in your source code, you can do what Markus
suggested, i.e. use an ordinary 2D array and specify the row length explicitly

int intArray[][5] = {
{ 1, 2, 1, 2, 1 },
{ 11 },
{ 0 }
};
 
H

hdante

Andrey said:
literals in C++, the row arrays will have to be declared explicitly:

int row1[] = { 1, 2, 1, 2, 1};
int row2[] = { 11 };
int row3[] = { 0 };

int* intArray[] = { row1, row2, row3 };


For this to be useful, you might need to know sizeof(row1),
sizeof(row2), ... without actually calling sizeof(row1), sizeof(...).
Is this possible ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top