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.
The name of the array arr is a pointer to the 0th 2-D array. Thus the pointer expression *(*(*(arr + i ) + j ) + k) is equivalent to the subscript expression arr[i][j][k]. We know the expression *(arr + i) is equivalent to arr[i] and the expression *(*(arr + i) + j) is equivalent arr[i][j].
int *var_name[array_size]; Declaration of an array of pointers: int *ptr[3]; We can make separate pointer variables which can point to the different values or we can make one integer array of pointers that can point to all the values.
An element in a multidimensional array like two-dimensional array can be represented by pointer expression as follows: **a+i+jIt represent the element a[i][j]. The base address of the array a is &a[0][0] and starting at this address the compiler allocates contiguous space for all the element row-wise.
const int someheight = 3; const int somewidth = 5; int (*array)[somewidth] = new int[someheight][somewidth];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With