Dynamically allocating a 2D array in C++ | Sololearn: Learn to code for FREE!
+ 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

1st Jan 2020, 8:32 AM
Shahil Ahmed
Shahil Ahmed - avatar
1 Answer
+ 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
1st Jan 2020, 8:59 AM
你知道規則,我也是 - avatar