generating integer array

Hi,i am new to c++ and i want to write a programm to generate an integer array. I keep getting the error at line 25: invalid types 'int[int]' for array . can anyone tell me what's wrong here. Thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main()
{
    int rows;
    int cols;
    cin>>rows>>cols;
    int test[rows][cols];
    get_test(rows,cols,&test[0][0]);
    cout<<test[1][1]<<endl;
    return 0;
}
int get_test(int rows,int cols,int *test)

{

int h=rows;
int w=cols;
int i=0,j=0;


for(i=0;i<h;i++)
{
    for (j=0;j<w;j++)
    {

        test[i][j]=i;
    }


}

return 0;
}


Line 6int test[rows][cols];

will not work, the compiler does not know how much space to allocate to the array because it is defined by the user.

Line 11, this the main problem you were asking about

int get_test(int rows,int cols,int *test)

int *test is a pointer to an int but below you do:

test[i][j]=i; which is a 2 dimension array, not an int.

To fix it in line 11 do:

(int rows,int cols,int *test[][])
Last edited on
What compiler do you have ? No modern compiler will let you compile that code. Look at line 6.
Topic archived. No new replies allowed.