下面的一段程序是C PRIMER PLUS中的一段例题,在FREEBSD系统中提示Segmentation fault (core dumped),这是咋回事啊??????
1 #include <stdio.h>
2 #define ROWS 3
3 #define COLS 4
4 int sum2d(int rows,int cols,int ar[rows][cols]);
5 int main (void)
6 {
7 int i,j;
8 int rs=3;
9 int cs=10;
10 int junk[ROWS][COLS]={{2,4,6,8},{3,5,7,9},{12,10,8,6}
};
11
12 int morejunk[ROWS-1][COLS+2]={{20,30,40,50,60,70},{5,
6,7,8,9,10}};
13
14 int varr[rs][cs];
15 for (i=0;i<rs;j++)
16 varr[i][j]=i*j+j;
17 printf("3*5 array\n");
18 printf("sum of all elements=%d\n",sum2d(ROWS,COLS,junk
));
19 printf ("2*6 array\n");
20 printf ("sum of all elements=%d\n",sum2d(ROWS-1,COLS+2
,morejunk));
21 printf("3*10 VLA\n");
22 printf("sum of all elements=%d\n",sum2d(rs,cs,varr));
23 return 0;
24 }
25
26 int sum2d(int rows,int cols,int ar[rows][cols])
27 {
28 int r,c;
29 int tot=0;
30 for (r=0;r<rows;r++)
31 for (c=0;c<cols;c++)
32 tot+=ar[r][c];
33 return tot;
34 }
1 #include <stdio.h>
2 #define ROWS 3
3 #define COLS 4
4 int sum2d(int rows,int cols,int ar[rows][cols]);
5 int main (void)
6 {
7 int i,j;
8 int rs=3;
9 int cs=10;
10 int junk[ROWS][COLS]={{2,4,6,8},{3,5,7,9},{12,10,8,6}
};
11
12 int morejunk[ROWS-1][COLS+2]={{20,30,40,50,60,70},{5,
6,7,8,9,10}};
13
14 int varr[rs][cs];
15 for (i=0;i<rs;j++)
16 varr[i][j]=i*j+j;
17 printf("3*5 array\n");
18 printf("sum of all elements=%d\n",sum2d(ROWS,COLS,junk
));
19 printf ("2*6 array\n");
20 printf ("sum of all elements=%d\n",sum2d(ROWS-1,COLS+2
,morejunk));
21 printf("3*10 VLA\n");
22 printf("sum of all elements=%d\n",sum2d(rs,cs,varr));
23 return 0;
24 }
25
26 int sum2d(int rows,int cols,int ar[rows][cols])
27 {
28 int r,c;
29 int tot=0;
30 for (r=0;r<rows;r++)
31 for (c=0;c<cols;c++)
32 tot+=ar[r][c];
33 return tot;
34 }
转载于:https://blog.51cto.com/coooo/200001