0

There is a similar topic about this. How do I declare a 2d array in C++ using new?

What I want to do is to create multiple 2d arrays according to some integer which determines how many 2d arrays there should be.

I want to create a single dimensional array first for pointers and assing every pointer to a multidimensional array, using new. But it seems like you can't ask for memory to create multidimensional array. Why can't we just write:

int** howManyPointers = new int*[translate];
for (int i = 0; i < translate; i++){
        howManyPointers[i] = new char[rowsUsed][2000];
    }

In my project, 2d array must have 2000 columns but row size is undetermined first. It will be given by the user. Assume you've already got it [rowsUsed]

So what?

Community
  • 1
  • 1
K.Yazoglu
  • 207
  • 3
  • 13

1 Answers1

0

You allocate array of pointers, and then for each pointer you allocate 1d array like this:

int** 2dArray = new int*[rows];
for (int i = 0; i < rows; ++i) {
    2dArray[i] = new int[cols];
}

and then you can do 2dArray[X][Y] for each X < rows and Y < cols;

Starl1ght
  • 4,422
  • 1
  • 21
  • 49