How to pass an array as argument with the user defining the column number?

Asked

Viewed 185 times

4

I’ll make a simple program just to illustrate my problem.

#include<iostream>
using namespace std;

void recebeValor( int mat[][col]) // me da erro de comp;
{
   mat[0][0] = 2;
   cout << mat[0][0];
}

int main()
{
cout << "Entre o numero de linhas da matriz A: ";
int lin;
cin >> lin;

cout << "Entre o numero de colunas da matriz A> ";
int col;
cin >> col; 

int mat[lin][col];

recebeValor(mat); // isso me da erro de compilacao
  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

4

Decide whether to use C or C++. In C you can do this in most compilers:

void recebeValor(int size, int mat[][size]) {
   mat[0][0] = 2;
}

int main() {
    int lin = 3;
    int col = 3;
    int mat[lin][col];
    recebeValor(col, mat);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

You can also use only pointers instead of array and works for C++.

If you will use C++, do not use array brute.

You can even create a gambiarra to settle this with template. But the real solution is to use a more abstract and more prepared structure to deal with this. Prefer to use a Vector.

#include<iostream>
#include <vector>
using namespace std;

void recebeValor(vector<vector<int>> mat) {
   mat[0][0] = 2;
   cout << mat[0][0];
}

int main() {
    cout << "Entre o numero de linhas da matriz A: ";
    int lin;
    cin >> lin;
    cout << "Entre o numero de colunas da matriz A> ";
    int col;
    cin >> col; 
    vector<vector<int>> mat(lin, vector<int>(col));
    recebeValor(mat);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

3

The problem is you didn’t declare col nowhere. Because of this the compiler will complain because mat is a parameter, but col he doesn’t know what it is.

Also, in C++, if you want to set the size of the matrix in the parameter, you have to do it statically. That is, the exact size has to be known at compile time. The reason is that in order for the compiler to be able to type check, it must have complete knowledge of the type at compile time, and therefore the type cannot depend on something that only exists during execution.

And more importantly: In order for the compiler to calculate how much memory space each element will occupy, he will need to know at compile time what size each position of the array parameter occupies. Since the type of the parameter is an array (array of arrays), then the compiler necessarily needs to know the exact size at compile time of all dimensions except the first.

So this is not valid:

int a, b, c;

// Erro, os valores a, b e c não estão disponíveis em tempo de compilação, só em tempo de execução.
void funcaoQualquer(int mat[a][b][c]) {
}

But this is valid, because the compiler knows exactly what the size of each dimension is:

void funcaoQualquer(int mat[5][5][5]) {
}

That’s valid from here, too. Although the compiler does not know the size of the array as a whole, he knows the size of each element in the first dimension.

void funcaoQualquer(int mat[][5][5]) {
}

Already these here are not valid. The compiler has no way of knowing what is the size of each element in the first dimension:

void funcaoQualquer1(int mat[][][5]) {
}

void funcaoQualquer2(int mat[][][]) {
}

void funcaoQualquer3(int mat[5][][]) {
}

void funcaoQualquer4(int mat[5][5][]) {
}

void funcaoQualquer5(int mat[][5][]) {
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.