+ 2
Dynamically allocating a 2D array in C++
I know how to allocate a array in C++ in runtime but when I try to do it with a 2D one it pops put an error , any solution? Code -> https://code.sololearn.com/c9oz4gj1j29U/?ref=app
1 ответ
+ 4
Dynamic 2D array is "an array of pointers to arrays". You need to declare double pointer and initialize with loops.
int** arr = new int[x];
for(int i = 0 ; i < y ; ++i)
arr[i] = new int[y];
//Note arr[i] is still a pointer.
And the same on deleting. Delete it by loop.
This question is asked in StackOverFlow
https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new