How do I pass an array as an argument to the user by setting the number of columns?

4

I will make a simple program just to exemplify 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
    
asked by anonymous 23.11.2015 / 20:29

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);
    return 0;
}

See running on ideone .

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

If you are going to use C ++, do not use raw array .

You can even create a gambiarra to resolve this with template . But the solution is to use a more abstract and more prepared structure to deal with it. Prefer to use 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);
    return 0;
}

See running on ideone .

    
23.11.2015 / 21:09
3

The problem is that you have not declared col anywhere. Because of this the compiler will complain because mat is a parameter, but col it does not know what it is.

In addition, in C ++, if you want to set the size of the array 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 for the compiler to be able to do type checking, it must have full knowledge of the compile-time type, so the type can not depend on something that only exists during execution.

And most importantly: In order for the compiler to be able to calculate how much memory each element will occupy, it 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 compile-time size of all dimensions except the first one.

So, this is not valid here:

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]) {
}

This is true, because the compiler knows exactly what the size of each dimension is:

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

This is also valid here. Although the compiler does not know the size of the array as a whole, it knows the size of each element in the first dimension.

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

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

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][]) {
}
    
23.11.2015 / 21:10