C – allocating a matrix in a function

You need to change

if((*matrix[i] = (int*)malloc(col * sizeof(int))) == NULL)

to

if(((*matrix)[i] = (int*)malloc(col * sizeof(int))) == NULL)
//  ^       ^

You need to dereference matrix before using the array subscript.

*matrix[i] is equivalent to *(matrix[i])

Leave a Comment