+ 2

can I not allocate a multi-dimensional array in c++?

25th Aug 2022, 5:31 PM
Unwanted Blue
Unwanted Blue - avatar
3 odpowiedzi
+ 1
Unwanted Blue In C++, you can create any program, but remember: as a developer, you are responsible for the program you create. In the attached codes, in the first one, I explained why the compiler throws an error and what needs to be done to make a two-dimensional pointer to an array. In the second one, I wrote clean code without explanation. I hope it helped you, if you have any questions, ask, I will be happy to answer them! Have a nice day and have a good study! In conclusion, I recommend avoiding using pointers to pointers unless no other options are available, because they’re complicated to use and potentially dangerous. It’s easy enough to perform indirection through a null or dangling pointer with normal pointers — it’s doubly easy with a pointer to a pointer since you have to do a double-indirection to get to the underlying value! https://code.sololearn.com/cWHUdHRc2dlx/?ref=app https://code.sololearn.com/cS9EV6EcUTB9/?ref=app
26th Aug 2022, 5:10 AM
Vlad Apet
+ 2
Yes, you can. In fact, it depends on the task. As an analog you can declare a multi-dimensional array with size bigger than required ( e. g. you need to create y arrays that contain n elemets. So you create int Array = new int[1000][1000] for any case. Or you can create n arrays: int n; cin >> n; for(int i = 0; i < n; i++){ int arr = new int[i][1000]; } I hope it helped. If you still have any questions, feel free to ask them, I’ll be glad to answer them! Have a good day!
25th Aug 2022, 7:38 PM
Vlad Apet
26th Aug 2022, 1:25 AM
Unwanted Blue
Unwanted Blue - avatar