Malloc a 2D array in C
like this : int (*arr)[M] = malloc(sizeof(int[N][M]));
arr
is pointer to int[M]
.
use like arr[0][M-1];
and free(arr);
int ** arr = malloc(N*sizeof(int[M]));
is incorrect C code, if you simulate it by allocating onceint *arr = malloc(N*M*sizeof(int));
and access it byarr[i*M + j]
,
this is an analog to arr[I][j]
in your first case.