3

i am new to c++ and i want to write a program to generate an integer array. I keep getting the error at line

test[i][j]=i;

invalid types 'int[int]' for array 

Can anyone tell me what's wrong here? Thanks in advance.

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;
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
newww0
  • 181
  • 3
  • 12
  • `int get_test(int rows,int cols,int *test)` don't think you want test to be a 1d array – Kevin L Jul 14 '14 at 13:44
  • Extra note you don't need to specify first element of array, just `get_test(rows,cols,test);` enough – ifyalciner Jul 14 '14 at 13:46
  • Instead of an array of arrays, you're usually better off with a small wrapper around a vector, such as one as posted in [a previous answers](http://stackoverflow.com/a/6465254/179910). Some libraries (e.g., [Boost](http://www.boost.org/doc/libs/1_55_0/libs/multi_array/doc/)) contain implementations of the same idea, so it's pretty easy to use them if you prefer not to write one (though for a task this trivial, I find the gain from using a library fairly minimal). – Jerry Coffin Jul 14 '14 at 14:19

3 Answers3

11

int test[rows][cols]; with non compile time value is a variable length array which is a possible extension of some compilers.

Prefer using std::vector instead:

int get_test(std::vector<std::vector<int>>& test)
{ 
    for (int i = 0;i != test.size(); ++i)
    {
        for (int j = 0; j != test[i].size(); ++j)
        {
            test[i][j] = i;
        }
    }
    return 0;
}

int main()
{
    int rows;
    int cols;
    cin>>rows>>cols;
    std::vector<std::vector<int>> test(rows, std::vector<int>(cols));
    get_test(test);
    cout << test[1][1] << endl;
    return 0;
}
user703016
  • 37,307
  • 8
  • 87
  • 112
Jarod42
  • 203,559
  • 14
  • 181
  • 302
-2

Your type for the array is wrong, you want something like:

 int get_test(int rows,int cols,int **test)
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
-3

The issue has to deal with your declaration of the arrays:

int rows;
int cols;
cin>>rows>>cols;
int test[rows][cols];

The compiler doesn't know the value of the integers at compile time. Therefore, it does not know how much space to allocate in memory.

Try allocating a bunch of space, more than you think you probably may need, such as:

int rows = 100;
int cols = 100;
int test[rows][cols] //assumes a maximum of size 100 for each row & col

Or using a dynamic array:

int **test = new int *[rows];
for(int i = 0; i < rows; i++)
test[i] = new int[cols];
bbraun17
  • 31
  • 5
  • Why would you allocate _more_ than you think you need? – Lightness Races in Orbit Jul 14 '14 at 14:44
  • This person is obviously a beginner to C++. I just want to give them the quick and easy answer to their question. It isn't necessary to give a complicated solution using dynamic vectors and whatnot. They will later learn more about better memory allocation later. For the sake of this example, this is an acceptable answer. – bbraun17 Jul 14 '14 at 18:50
  • Vectors _are_ "the quick and easy answer". Manual memory management, with all its pitfalls, is inordinately more complicated and in this instance you're actually teaching _bugs_ (e.g. lots of memory leaks!). How can that be reasonable?! – Lightness Races in Orbit Jul 14 '14 at 19:36