Create a two-dimensional array with new

Asked 2 years ago, Updated 2 years ago, 110 views

How do I make a two-dimensional array with new?

When it's a one-dimensional arrangement, int* ary = new int[Size] I wrote it like this

in a two-dimensional array int** arr = new int[sizeX][sizeY];

I can't compile it like this!

c++ array

2022-09-22 22:31

1 Answers

To dynamically assign a two-dimensional array, you must write a repeat statement as follows:

int sizeX = 5;
int sizeY = 10;

int** ary = new int*[sizeX];
for(int i = 0; i < sizeY; ++i)
    ary[i] = new int[sizeY];

If you look at the picture, it's easy to understand.

alt text


2022-09-22 22:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.