How do i declare a 2d array using new?
Like, for a "normal" array I would:
int* ary = new int[Size]
but
int** ary = new int[sizeY][sizeX]
a) doesn t work/compile and b) doesn t accomplish what:
int ary[sizeY][sizeX]
does.
Sommaire |
How do i declare a 2d array using new?
Like, for a "normal" array I would:
int* ary = new int[Size]
but
int** ary = new int[sizeY][sizeX]
a) doesn t work/compile and b) doesn t accomplish what:
int ary[sizeY][sizeX]
does.
A dynamic 2D array is basically an array of pointers to arrays . You should initialize it using a loop, like this:
int** ary = new int*[rowCount]; for(int i = 0; i < rowCount; ++i) ary[i] = new int[colCount];
The above, for colCount= 5 and rowCount = 4, would produce the following:
<img src="https://i.stack.imgur.com/M75kn.png" alt="enter image description here">
License : cc by-sa 3.0
http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new