[Solved] Segmentation fault occurs in C program


As sjsam pointed out, this statement is wrong:

arr=(int **) malloc(sizeof(int )*n);

Your program works on systems where sizeof(int) == sizeof(int*) (i.e. 32-bit systems), but will likely fail on 64-bit ones (on which sizeof(int) == 4 and sizeof(int*) == 8.

i want to create a 2d array dynamically of dimension n by q

We understand that, and you have created such an array here:

arr[0] = (int *) malloc(sizeof(int )*q*n);

But you are creating something else on the problem statement; namely an array of n pointers, not an array of n integers.

1

solved Segmentation fault occurs in C program