# 2d array with new[]

This is a discussion on 2d array with new[] within the C++ Programming forums, part of the General Programming Boards category; I use a 2d array and I declare it like this: Code: double * table = new double[2][num]; the integer ...

1. ## 2d array with new[]

I use a 2d array and I declare it like this:

Code:
`double * table = new double[2][num];`
the integer "num" is entered by the user. Dev-C++ gives errors:

functions.h:47: cannot convert `double (*)[((num - 1) + 1)]' to `double*' in
initialization
What is wrong?

2. Well as far as I know, you can only make 1D arrays dynamically.

Since you know the size of one bit of it, why not just create an array of pointers like this:
Code:
`double *table[2];`
Then you just go through each element of the array and create a new double of length 'num':
Code:
```for (int x = 0; x < 2; x++)
table[x] = new double[num];```

3. Well, I suppose you could do something like this:
Code:
```const int table_size = 2;
double* table[table_size];

for (int i = 0; i < table_size; ++i) {
table[i] = new double[num];
}

// ...

for (int i = 0; i < table_size; ++i) {
delete[] table[i];
}```
Have you considered using vectors?

4. > double * table = new double[2][num];
You can only do multiple dimensions like this if all the minor dimentions are compile-time constants.

Eg.
double (*table)[2] = new double[num][2];

5. Thanks for the replies!

@laserlight:
I don't know how to use vectors yet

6. If you are learning C++, many would suggest learning vectors before C style arrays.

Popular pages Recent additions