Alex Rivera | Logout

Declaring a pointer to multidimensional array and allocating the array

Asked 2010-10-11T07:22:30.787
28

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.

Edit
Report

1 Answer

52
const int someheight = 3;
const int somewidth = 5;

int (*array)[somewidth] = new int[someheight][somewidth];
answered 2010-10-11T07:34:21.533

Your Answer