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;
}
3
  • int get_test(int rows,int cols,int *test) don't think you want test to be a 1d array Commented Jul 14, 2014 at 13:44
  • Extra note you don't need to specify first element of array, just get_test(rows,cols,test); enough Commented Jul 14, 2014 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. Some libraries (e.g., Boost) 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). Commented Jul 14, 2014 at 14:19

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

-2

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

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

2 Comments

I don't think that multi-dimensional VLA can be passed by this way.
This is wrong, even if you ignore the fact that it promotes writing really poor code for no reason. Multidimensional arrays cannot be used as pointers to pointers.
-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];

3 Comments

Why would you allocate more than you think you need?
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.
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?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.