Pass pointer as argument to a function

1

I want to create an array using pointer pointers, however I'm having a hard time passing the parameters. First I dynamically allocate the array using a function. And then I read it. The program compiled, however at the time of reading the values it stops. I think the error is in the parameter pass, but I can not 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;
 }
    
asked by anonymous 06.09.2017 / 22:48

2 answers

1

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

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

You are changing a copy and not the original pointer that is in main .

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

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

What on my machine gave

Insteadyoucanmakethefunctionreceivetheaddressofthearraysothatitcanchangeitsvalue:

intalocaMatriz(int***matriz,intlin,intcol){//agoracomponteiroparaint**inti;*matriz=malloc(lin*sizeof(int*));//com*alterarovalorapontadofor(i=0;i<col;i++){(*matriz)[i]=(int*)malloc(col*sizeof(int));//aquitambém}return0;}

Nowcallingmainwithyouraddresslikethis:

alocaMatriz(&matriz,lin,col);

Withthesamepointerprinttestitisalsoclearthatithasnowbeenaltered:

Both the leMatriz function and mostraMatriz do not need to be changed because they do not change the matriz pointer itself but the values it points to, that is, its contents.

    
06.09.2017 / 23:44
1

The problem is with memory allocation. If you want to allocate a pointer to pointer you have to declare it like this.

Also understand that this is a C code compiled with C ++ and not a C ++ code. I'd rather do C in compiler C 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);
}

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

    
06.09.2017 / 23:06