I know this question was answered but it somehow doesn't work for me. I dynamically allocate memory for array like this:
arr = (int**)(malloc(rows*sizeof(int*)));
arr[0] = (int*)(malloc(rows*columns*sizeof(int))); // <-- THIS DOESN'T WORK
printf("\nArray: \n");
while(fscanf(fp, "%d", &num) == 1) {
//arr[row] = (int*)(malloc(columns*sizeof(int))); <---THIS WORKS
arr[row][col] = num;
printf("%d ", arr[row][col]);
if((++col == columns)){
row++;
col = 0;
printf("\n");
}
}
It throws segmentation fault after 4th row, if matrix is 6x6. Any advice? Thanks
EDIT: See: http://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/ point number 4. If I do it like in point 3, it works. But I need it like in 4.