# pointer to multidimensional array?

This topic is 4904 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to be able to create a pointer to a multidimensional array of unknown size. I tried this but it doesn't seem to work. Is it even possible? int **array_ptr; int array[][] = {{0, 0, 0, 0,}, {0,0,0,0}}; array_ptr = array;

##### Share on other sites
It is not possible the way you are doing it. An array created using A a[ ][ ] is interpreted by the compiler as: a[ i ][ j ] = a[ i+j*w ] where w is the width of the array. Also, this means a is of type A*, not A**. Since there is no way to extract a "width" value from a pointer, you cannot use a pointer like this.

However, there are other ways to do this. You can:
1- store the width yourself

A * a = get2DArray( ); //use a function to get the array
a[ i + w*j ]; //instead of a[ i ][ j ]

2- allocate an array of arrays

A * intermediary = new Array[ h*w ];
A ** a = new Array[w];
// set a[ i ] to (&intermediary[ i*h ])
// so that a[ i ][ j ] == intermediary[ i*h+j ]
a[ i ][ j ];

3- use a vector of vectors

std::vector< std::vector< A > > a;
//initialize everything to the correct size: a of size w, a[ i ] to size h
a[ i ][ j ];

4- create a wrapper

//use an "A2D" class to wrap around A * a', modify operator()
a(i,j); // == a.a'[ i+(a.w)*j ] where a is an instance of A2D.

5- use boost::multi_array

// from here.

boost::multi_array< A,2 > a(boost::extents[ w ][ h ]);
a[ i ][ j ];

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 27
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001791
×