Declaring a pointer to multidimensional array and allocating the array
I've tried looking but I haven't found anything with a definitive answer. I know my problem can't be that hard. Maybe it's just that I'm tired..
Basically, I want to declare a pointer to a 2 dimensional array. I want to do it this way because eventually I will have to resize the array. I have done the following successfully with a 1D array:
int* array;
array = new int[somelength];
I would like to do the following with a 2D array but it won't compile:
int* array;
array = new int[someheight][somewidth];
The compiler gives me an error stating that ‘somewidth’ cannot appear in a constant-expression. I've tried all sorts of combinations of ** and [][] but none of them seem to work. I know this isn't that complicated...Any开发者_开发问答 help is appreciated.
const int someheight = 3;
const int somewidth = 5;
int (*array)[somewidth] = new int[someheight][somewidth];
I just found this ancient answer still gets read, which is a shame since it's wrong. Look at the answer below with all the votes instead.
Read up on pointer syntax, you need an array of arrays. Which is the same thing as a pointer to a pointer.
int width = 5;
int height = 5;
int** arr = new int*[width];
for(int i = 0; i < width; ++i)
arr[i] = new int[height];
A ready to use example from here, after few seconds of googling with phrase "two dimensional dynamic array":
int **dynamicArray = 0;
// memory allocated for elements of rows.
dynamicArray = new int *[ROWS];
// memory allocated for elements of each column.
for( int i = 0 ; i < ROWS ; i++ ) {
dynamicArray[i] = new int[COLUMNS];
}
// free the allocated memory
for( int i = 0 ; i < ROWS ; i++ ) {
delete [] dynamicArray[i];
}
delete [] dynamicArray;
I suggest using a far simpler method than an array of arrays:
#define WIDTH 3
#define HEIGHT 4
int* array = new int[WIDTH*HEIGHT];
int x=1, y=2, cell;
cell = array[x+WIDTH*y];
I think this is a better approach than an array of an array, as there is far less allocation. You could even write a helper macro:
#define INDEX(x,y) ((x)+(WIDTH*(y)))
int cell = array[INDEX(2,3)];
Personally, my preference is to use a syntactic trick to declare a pointer to the dynamically sized multi-dimensional array. This works in compilers that support Variable Length Arrays (VLAs), which all C++ compilers should, and most current C compilers.
The basic idea is captured in this:
void bar (int *p, int nz, int ny, int nx) {
int (*A)[ny][nx] = (int(*)[ny][nx]) p;
"p" points at the (contiguous) block of space you want to treat as a multi-dimensional array. "A" has the same value as "p", but the declaration makes the compiler treat references to "A" in the multi-dimensional way you want. For example:
#include <iostream>
using namespace std;
void bar (int *p, int nz, int ny, int nx)
{
int (*A)[ny][nx] = (int(*)[ny][nx]) p;
for (int ii = 0; ii < nz; ii++) {
for (int jj = 0; jj < ny; jj++) {
for(int kk = 0; kk < nx; kk++) {
A[ii][jj][kk] = ii*1000000 + jj*1000 + kk;
}
}
}
}
void out (int *p, int nz, int ny, int nx)
{
int (*A)[ny][nx] = (int(*)[ny][nx]) p;
cout << A[11][22][33] << endl;
}
int main (void)
{
int NX = 97;
int NY = 92;
int NZ = 20;
int *space = new int [NZ * NY * NX];
bar (space, NZ, NY, NX);
out (space, NZ, NY, NX);
return 0;
}
Running this produces the output "11022033"
The declaration of the "A" alias is a little weird looking, but it allows you to directly and simply use the desired multi-dimensional array syntax
I think this will do
int r, c ;
std::cin>>r>>c ;
int *array = new int[r*c] ;
You can input the values by doing something like this
for (int i = 0 ; i < r ; i++){
for (int j = 0 ; j < c ; j++){
std::cin>>array[i *c + j] ;
}
}
精彩评论