1

This is the code I wrote to take the values of a matrix and display it

#include<stdio.h>   

int ** readMatrix(int rows, int cols)
{
    int i,j, matrix[rows*cols];
    int *b[rows];
    int **y=b;

    int k=0;
    for(k=0; k < rows; k++)
    {
        b[k]=&matrix[k*cols];
    }

    for(i=0; i< rows*cols; i++)
    {
        scanf("%d", matrix+i);
    }
    return y;
}

void displayMatrix(int **a, int rows, int cols)
{
    int k=0,j;
    for(k=0; k < rows; k++)
    {
        for(j=0; j < cols; j++)
        {
            printf("%d ", *(*(a + k) + j));
        }
        printf("\n");
    }
}

int main()
{   
    int rows,cols;
    printf("Enter the number of rows:\n");
    scanf("%d",&rows);
    if(rows <= 0)
    {
        printf("Invalid Input");
    }
    else
    {
        printf("Enter the number of columns:\n");
        scanf("%d",&cols);
        if(cols <= 0)
        {
            printf("Invalid Input");
        }
        else
        {
            printf("Enter the values:\n");
            int **a = readMatrix(rows, cols);
            displayMatrix(a, rows, cols);
        }
    }
}

The program is getting stuck at the loop in displayMatrix, but it displays fine if I remove the outer for loop.

The error I get is Segmentation fault (core dumped).

What am I doing wrong?

PS: I have to use the above function signature with double pointers.

Bob__
  • 12,361
  • 3
  • 28
  • 42
  • 1
    `matrix` is a local variable of `readMatrix`, so its lifetime is limited to the scope of that function. – Bob__ Jun 07 '17 at 08:22
  • 1
    Please, also note that you don't have a 2d-array anywhere in your code. `matrix` is 1d VLA, while the other variables you are passing around are pointers to pointers. – Bob__ Jun 07 '17 at 08:32
  • In your `diaplayMatrix` function, you can use `a[k][j]` rather than `*(*(a + k) + j)`. They do the same thing, but the first one is easier to read. I'd also recommend using variable names `i` and `j` rather than `k` and `j`, just for the sake of convention. – kamoroso94 Jun 07 '17 at 09:05

1 Answers1

1

Your problem in readMatrix is that it returns a pointer to a local variable in that function. You're not supposed to do that, because local variables get deallocated after the function returns, which is why you're getting a segmentation fault.

You need to create one with malloc in the function (and remember to free it). You will need to put #include <stdlib.h> at the top with the other headers to access these functions. You can see the changes below to make this work.

int** readMatrix(int rows, int cols) {
    int i, j;
    int **matrix = malloc(rows * sizeof(int*));

    for(i = 0; i < rows; i++) {
        matrix[i] = malloc(cols * sizeof(int));
        for(j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    return matrix;
}

Now it may be helpful to have a function for freeing this data structure. Here's how you can do that.

void freeMatrix(int **matrix, int rows) {
    int i;
    for(i = 0; i < rows; i++) {
        free(matrix[i]);
    }
    free(matrix);
}

Then change the main function to accommodate these changes as follows.

int **a = readMatrix(rows, cols);
displayMatrix(a, rows, cols);
freeMatrix(a, rows);
kamoroso94
  • 1,713
  • 1
  • 16
  • 19
  • i read that variables get deallocated but the memory holds the values so we can access the values if we have the memory locations. Also the same code without using the array of pointers (b) in readMatrix and the outer loop in displayMatrix works fine. – Saket Shrivastava Jun 07 '17 at 09:29
  • @SaketShrivastava theoretically, yes, but I don't recommend that. I'm pretty sure it's undefined behavior. – kamoroso94 Jun 07 '17 at 09:31
  • int ** readMatrix(int rows, int cols) { int i,j, matrix[rows*cols]; int *b; b = matrix; int **y=&b; for(i=0; i< rows*cols; i++) { scanf("%d", matrix+i); } return y; } void displayMatrix(int **a, int rows, int cols) { int i=0,j; int k; k=i*cols; for(j=0; j < cols; j++) { printf("%d ", *(*a + j + k)); } printf("\n"); i=i+1; } – Saket Shrivastava Jun 07 '17 at 09:35
  • this is what i was doing first. as you can see in the display matrix part, there is a k. if i manually change the values, it gives output but using loop to change k again gives error. – Saket Shrivastava Jun 07 '17 at 09:37
  • [link]https://ibb.co/fxyjvv this is what i am doing – Saket Shrivastava Jun 07 '17 at 09:52