Declaring a multidimensioal array with variables?

I was writing a program with a 2D array (see below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 #include <iomanip>
#include <iostream>
using namespace std;

// ------ Prototypes ------------------------------------------------------
void getData(double [][4], int, int);

// ****** MAIN ************************************************************
int main()
{
    int div = 6;
    int sales = 4;

    double division[div][sales];

    getData(division, div, sales);


Every time I tried to run the program is gave me an error. When I changed the column ex: division[div][4], it ran just fine. Is this just part of how multi D arrays work? I saw no mention of it my book. Could someone give an explanation?

Just to recap

function(array[2][3]) Works
function(array[variable][3]) Works
function(array[2][variable]) ERROR
function(array[variable][variable]) ERROR
The problem is that you can't declare an array with dynamic size. To do this, you should use a vector. You can read about vectors in the documentation on this website.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Doesn't work
int div = 6;
int sales = 4;

double division[div][sales];

//Does work
const int div = 6;
const int sales = 4;

    double division[div][sales];

//Also works 
int div = 6;
int sales = 4;

double division[6][4];
Topic archived. No new replies allowed.