Pass pointer pointer as argument for a function

Asked

Viewed 1,641 times

2

I want to create an array using pointer pointer, but I’m having difficulty passing the parameters. First I dynamically allocate the matrix using a function. And then I read. The program compiled, but when reading the values it stops. I think the error is in the parameter passage, but I can’t see.

int alocaMatriz(int **matriz, int lin, int col){
     int i;

     matriz=(int**)malloc(lin*sizeof(int*));

     for(i=0;i<col;i++){
         matriz[i]=(int*)malloc(col*sizeof(int));
     }
}

void leMatriz(int **matriz, int lin, int col){
     int i, j;

     printf("\nDigite os valores: ");

     for(i=0;i<lin;i++){
        for(j=0;j<col;j++){
             scanf("%d", &matriz[i][j]);
         } 
     }
 }

int main(int argc, char** argv) {
     int lin, col;
     int **matriz;

     printf("Digite as dimensoes da matriz: ");
     scanf("%d %d", &lin, &col);

     alocaMatriz(matriz, lin, col);
     leMatriz(matriz, lin, col);
     mostraMatriz(matriz, lin, col);

     return 0;
 }

2 answers

3

The problem is in memory allocation. If you want to allocate a pointer to pointer you have to declare so.

Also understand that this is a C code compiled with C++ and not a C++ code. I prefer to do C in C compiler and C++ in C compiler++.

#include <stdio.h>
#include <stdlib.h>
 
void alocaMatriz(int **matriz, int lin, int col){
    matriz = (int **)malloc(lin * sizeof(int **));
    for (int i = 0; i < col; i++) matriz[i] = (int *)malloc(col * sizeof(int));
}
 
void leMatriz(int **matriz, int lin, int col){
    printf("\nDigite os valores: ");
    for (int i = 0; i < lin; i++) for (int j = 0; j < col; j++) scanf("%d", &matriz[i][j]);
}
 
int main(int argc, char** argv) {
    int lin, col;
    int **matriz;
    printf("Digite as dimensoes da matriz: ");
    scanf("%d %d", &lin, &col);
    alocaMatriz(matriz, lin, col);
    leMatriz(matriz, lin, col);
}

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

3


The problem is in malloc within the alocaMatriz. When the function alocaMatriz is called is copied the value of int **matriz; who is in the main into the function, so the change within the function alocaMatriz:

int alocaMatriz(int **matriz, int lin, int col){
     ...
     matriz=(int**)malloc(lin*sizeof(int*)); //esta alteração
     ...

You are changing a copy, not the original pointer on main.

We can prove this by printing the pointer before and after allocation with %p, thus:

int **matriz;
printf("\n%p\n", matriz);
...
alocaMatriz(matriz, lin, col);
printf("\n%p\n", matriz);

In what my machine gave

inserir a descrição da imagem aqui

Instead you can make the function receive the address of the matrix so that it can change its value:

int alocaMatriz(int ***matriz, int lin, int col){ //agora com ponteiro para int**
    int i;

    *matriz=malloc(lin*sizeof(int*)); //com * alterar o valor apontado

    for(i=0;i<col;i++){
        (*matriz)[i]=(int*)malloc(col*sizeof(int)); //aqui também
    }
    return 0;
}

Calling now on main with your address like this:

alocaMatriz(&matriz, lin, col);

With the same pointer print test it is also clear that the same now has indeed been changed:

inserir a descrição da imagem aqui

Both the function leMatriz as the mostraMatriz do not need to be changed as they do not change the pointer matriz in itself but the values to which it points, namely its content.

Browser other questions tagged

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