Sid Vishnoi
Sid Vishnoi

Reputation: 1318

C++ 2D Array - Error invalid types ‘int[int]’ for array subscript

I am trying to create MxN matrix using 2D-arrays in C++.

The createMatrix() function asks for user input for matrix items and the printMatrix() function has to print the matrix.

But the printing task is not working (I can't access the array created, I don't understand why)

I receive the error :

matrix.cpp:35:20: error: invalid types ‘int[int]’ for array subscript
    cout << matrix[i][j];

The code I'm working with is:

#include "iostream"
using namespace std;

// user input matrix
int createMatrix(int m, int n){
    int arr[m][n];
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << "A[" << i << "][" << j << "] : ";
            cin >> arr[i][j];
        }
        cout << endl;
    }
    return arr[m][n];
}

/*
void printMatrix(int matrix[][2], int m, int n){
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << matrix[i][j];
        }
    }
}
*/

int main(){ 
    int m = 2, n = 2; // m = rows, n = columns
    int matrix = createMatrix(m,n);

    //  printMatrix(matrix, m, n); // not working as sub-routine too, main target to make it work with sub-routine

    // to print matrix
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << matrix[i][j];
        }
    }

    return 0;
}

Upvotes: 0

Views: 851

Answers (3)

Alex Lop.
Alex Lop.

Reputation: 6875

Yes, createMatrix works but you won't be able to do anything with what it created. Because:

  1. arr[n][m] is local (and out of boundaries by the way). It is not a matrix as you probably thought but an item of arr at position [n][m].
  2. It is not well defined to declare array of fixed sizes with vary sizes that depend on function input.

You need to pass to createMatrix array from the main() as pointer (like you did in printMatrix) and createMatrix should work with it and not something local.

Now regarding your original question:

But the printing task is not working (I can't access the array created, I don't understand why)

matrix was defined as int, not as array.

int matrix = createMatrix(m,n);

Upvotes: 0

NathanOliver
NathanOliver

Reputation: 180935

matrix is an int not an int[][]. Since it is an int there is no subscript operator and that is why you are getting the error you are getting. You are also using veriable length arrays which is not standard C++. I would suggest you change your code to use a std::vector like

std::vector<std::vector<int>> createMatrix(int m, int n)
{
    std::vector<std::vector<int>> arr(m, std::vector<int>(n));
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << "A[" << i << "][" << j << "] : ";
            cin >> arr[i][j];
        }
        cout << endl;
    }
    return arr;
}

And then main() would be:

int main(){ 
    int m = 2, n = 2; // m = rows, n = columns
    std::vector<std::vector<int>> matrix = createMatrix(m,n);

    //  printMatrix(matrix, m, n); // not working as sub-routine too, main target to make it work with sub-routine

    // to print matrix
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            cout << matrix[i][j];
        }
    }

    return 0;
}

Upvotes: 5

Michail
Michail

Reputation: 366

Your matrix is not array. it is int.

You need to work with the pointers.

Upvotes: 0

Related Questions