Malloc a 2D array in C [duplicate] Malloc a 2D array in C [duplicate] arrays arrays

Malloc a 2D array in C [duplicate]


int ** arr = malloc(N*sizeof(int[M])); is simply incorrect C code, if you simulate it by allocating once like this:

int *arr = malloc(N*M*sizeof(int));

Add access it by arr[i*M + j], this is analog to access arr[I][j] in your first case.


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);


You have a "pointer to pointer". That cannot represent a 2D array.

The correct declaration of a pointer to a 2D array is

// number of elements in one row#define COLS 10// number of rows#define ROWS 20int (*array)[COLS];   // mind the parenthesis!

That makes array a pointer to array of COLS ints. The type is `int (*)[COLS], btw. but you don't need the type, see below.

To allocate the array you should then use the standard allocation for a 1D array:

array = malloc(sizeof(*array) * ROWS);   // COLS is in the `sizeof`array = malloc(sizeof(int[ROWS][COLS])); // explicit 2D array notation

Which variant to use is personal style. While the first contains no redundancy (consider you change the declaration of array to use INNER instead of COLS or the element-type to float). The second is more clear at a fist glance, but more prone to errors when modifying the declaration of array.

To free:

free(array);