Dynamic array as a parameter in C or C ++?

1

After hours of research, I did not find anything that answered my question, or I could not understand the answer. I want to create a NxN array of variable size. I did with pointers , but I want to pass it as a parameter between functions, a function to fill, another to manipulate, etc. But it is not working

After creating the array, how do I pass it to another function? And what should be the statement of the other function to receive it?

int main()
{
int **campo;
...
campo = alocaMatriz(altura,largura);
popularMatriz(campo); //???e agora rsrs
outraFuncao(campo);//??!?!?!
}

int alocaMatriz(int linha, int coluna)
{
int i,j;
int **campo = (int**)malloc(linha * sizeof(int*)); 

for (i = 0; i < linha; i++)
{
    campo[i] = (int*) malloc(coluna * sizeof(int)); 
    for (j = 0; j < coluna; j++)
    {
        campo[i][j] = 0; //Inicializa com 0.
    }
}
campo[0][0]=1;//adiciona o agente ao tabuleiro
}
outraFuncao(int **campo){} //?????
    
asked by anonymous 25.03.2018 / 06:51

1 answer

1

The argument to the functions must be of type 'int **', if you are using a pointer to pointer representation. As a rule, the argument to the function must be of the same type as the data structure being passed, in this case a pointer to integer pointers.

Example:

void popularMatriz(int **campo);

int main(){
    int **campo = alocaCampo(linhas, colunas);
    popularMatriz(campo);
}
    
27.03.2018 / 14:19