0

I'm attempting to create a dynamic Matrix with functions, but first I decided to segment the assignment so that I know everything works as it should. But the creation is not working.

I tried moving the parentheses, using different values, but it always crashes when i=2

int rows=4;
int columns=4;
int cont=1;
int ** Mat;
Mat=(int**)malloc(rows*columns*sizeof(int));
for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < columns; j++)
    {
        printf("%d",i);
        *(*(Mat+i)+j)=cont++;
    }
}
for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < columns; j++)
    {
        printf("%2d ", *(*(Mat+i)+j) );  /* a[i][j] */
    }
    printf("\n");
}

I don't get any compilation errors, I would expect this 1234 5678 9101112 13141516

klutt
  • 30,332
  • 17
  • 55
  • 95
W. Blengio
  • 13
  • 4
  • Possible duplicate of [Dynamic Multidimensional array in C](https://stackoverflow.com/questions/8725364/dynamic-multidimensional-array-in-c) – stark Aug 22 '19 at 01:24
  • See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Aug 22 '19 at 07:43

2 Answers2

0

Change:

int ** Mat;
Mat=(int**)malloc(rows*columns*sizeof(int));

to:

int (*Mat)[columns];
Mat = malloc(rows * sizeof *Mat);

int ** Mat; declares Mat to be a pointer to a pointer to an int. But to make that work, you have to allocate space for a bunch of pointers (one for each row) and fill them in with pointers to space allocated for the rows. You did not do that.

You allocated space for rows * columns int, and you set Mat to point to that space. It seems like you want to use Mat as a pointer to a matrix (or, properly, to the first row of a matrix), not as a pointer to pointers. So declare it as int (*Mat)[columns];, which makes it a pointer to an array of four columns (specifically, it is pointing to the first such array, which is followed by others).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Perfect! Thanks a bunch for the explanation! The assignment wants us to only use pointers and not [] to loop through the matrix so I was really confused, but this helped a ton! – W. Blengio Aug 22 '19 at 01:37
  • How would you suggest I sent this to a function? Should I send it as int*Mat[]? – W. Blengio Aug 22 '19 at 01:48
  • @W.Blengio: Declare a function parameter the same way as the pointer: `int (*Mat)[columns]`. Make sure `columns` is an earlier parameter. – Eric Postpischil Aug 22 '19 at 01:54
  • @W.Blengio You can declare the function as `void func (int rows, int cols, int mat[rows][cols])`. This works because `int mat[rows][cols]` as a function parameter is equivalent to `int(*mat)[cols]`. Then call the function with `func(rows, cols, Mat)`, where `Mat` is a pointer of type `int (*Mat)[columns];`. – Lundin Aug 22 '19 at 07:45
0

Your implementation is using a jagged array. That is, the Mat is an array of pointers where each pointer points to a row. The only part of your code that is wrong is the initialization. When you create the pointer for your Mat, the pointers to the rows come from whatever memory malloc gave you, and thus or not valid. You need to initialize the pointer for each row.

Mat=(int**)malloc(rows*sizeof(int*));
for (int i=0;i<rows;++i) {
    Mat[i] = malloc(cols*sizeof(int));
}
Ted Brownlow
  • 1,103
  • 9
  • 15
  • 1
    This is worse than what the OP has, because it doesn't create a 2D array, but instead a slow, segmented look-up table. The correct solution is to change the `int**` to the correct pointer type. – Lundin Aug 22 '19 at 07:48