> mat1 = malloc(cols * sizeof(int*));
You've got the rows and cols mixed up in a couple of places.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
You're mixing up the meaning of a row and a column. The following allocates using column major order (a[cols][rows]):
mat1 = malloc(cols * sizeof(int*));
for(i = 0; i < cols; i++)
{
mat1[i] = malloc(rows * sizeof(int));
}
And this prints using row major order (a[rows][cols]):
for (i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
scanf("%d",&mat1[i][j]);
Unless rows and cols have the same value, the two aren't interchangeable and you're accessing memory outside the bounds of the array you just allocated. Linux is giving you a segmentation fault.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401