1

I was developing code to accept two matrices, dynamically allocate memory for them and display their sum using functions. Declaring the return types of my input,output and sum functions as 'void' resulted in a segmentation error, but, on setting the return type to 'int**' the code worked fine. I want to know what's the difference. Why was my code unable to access the elements of the matrices when the return type of the functions was 'void'?

I thought it should be able to since I had passed the pointer to the matrix as an argument to the function.

//Before changing the return type:

void input(int **x,int m,int n){
    int i,j;
    x=(int**)malloc(m*sizeof(int*));
    for(i=0;i<m;i++){
        x[i]=(int*)malloc(n*sizeof(int));
        for(j=0;j<n;j++){
            scanf("%d",&x[i][j]);
        }
    }
}
void sum(int **z,int **y,int **x,int m,int n){
    int i,j;
    z=(int**)malloc(m*sizeof(int*));
    for(i=0;i<m;i++){
        z[i]=(int*)malloc(n*sizeof(int));
        for(j=0;j<n;j++){
            z[i][j]=x[i][j]+y[i][j];
        }
    }
}
void output(int **x,int m,int n){
    int i,j;
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            printf("%d ",x[i][j]);
        }
        printf("\n");
    }
}


//After changing the return type:

int** input(int **x,int m,int n){
    int i,j;
    x=(int**)malloc(m*sizeof(int*));
    for(i=0;i<m;i++){
        x[i]=(int*)malloc(n*sizeof(int));
        for(j=0;j<n;j++){
            scanf("%d",&x[i][j]);
        }
    }
    return x;
}
int** sum(int **z,int **y,int **x,int m,int n){
    int i,j;
    z=(int**)malloc(m*sizeof(int*));
    for(i=0;i<m;i++){
        z[i]=(int*)malloc(n*sizeof(int));
        for(j=0;j<n;j++){
            z[i][j]=x[i][j]+y[i][j];
        }
    }
    return z;
}
void output(int **x,int m,int n){
    int i,j;
    for(i=0;i<m;i++){
        for(j=0;j<n;j++){
            printf("%d ",x[i][j]);
        }
        printf("\n");
    }
}
  • 3
    See https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function – Barmar Sep 10 '19 at 17:37
  • The posted code does not compile! mainly because the needed header files: `stdio.h` and `stdlib.h` have not been included – user3629249 Sep 10 '19 at 19:26
  • We cannot reproduce the problem without a `main()` function, which is missing from the posted code. – user3629249 Sep 10 '19 at 19:28
  • OT: regarding this kind of statement: `x=(int**)malloc(m*sizeof(int*));` 1) in C, the returned type is `void *` which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc 2) always check (!=NULL) the returned value to assure the operation was successful. 3) the function is expecting a parameter of type `size_t`, but the 'm' is an `int` – user3629249 Sep 10 '19 at 19:33
  • OT: regarding: `scanf("%d",&x[i][j]);` Always check the returned value (not the parameter values) to assure the operation was successful. Note: any returned value other than the number of 'input format conversion specifiers' indicates an error. Suggest: `if( scanf("%d",&x[i][j]) != 1 ) { // handle error }` – user3629249 Sep 10 '19 at 19:39
  • OT: it is good programming practice to limit the scope of variables. so in the function: `output()` the variables: `i` and `j` should be defined in the 'for()` code blocks. I.E. `if( int i=0; i – user3629249 Sep 10 '19 at 19:44

1 Answers1

1

When you pass a pointer to a function, you are basically passing the address of a variable. In your case, you're allocating memory inside the function which means the variable is assigned new memory space, thereby the address of the variable also changes which you do not have access to outside the function unless you return it.

If you're allocating memory to a pointer variable inside the function, then there's no point in passing the pointer to the function. You can just declare and initialise a pointer within your function and return it.

Suraj
  • 602
  • 3
  • 16