|  | 
 
| 
#include <stdio.h>
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  #define ROWS 3
 #define COLS 4
 int sum(int rows,int cols,int ar[rows][cols]);
 int main(void)
 {
 int i,j;
 int rs=3;
 int cs=10;
 
 int junk[ROWS][COLS]={{2,4,6,8},{3,5,7,9},{12,10,8,6}};
 
 
 int morejunk[ROWS-1][COLS+2]={{20,30,40,50,60,70},{5,6,7,8,9,10}};
 
 int varr[rs][cs];  //变长数组
 for (i=0; i<rs;i++)
 for (j=0; j<cs; j++)
 varr[i][j]=i*j+j;
 
 printf("3x4 array\n");
 printf("Sum of all elements=%d\n", sum(ROWS,COLS,junk));
 
 printf("2x6 array\n");
 printf("Sum of all elements=%d\n",  sum2d(ROWS-1,COLS+2,morejunk));
 
 
 printf("3x10 VLA\n");
 printf("Sum of all elements=%d\n", sum2d(rs,cs,varr));
 
 return 0;
 }
 
 int sum(int rows,int cols,int ar[rows][cols])
 //带一个变长数组(VLA)参数的函数
 {
 int r;
 int c;
 int tot=0;
 
 for(r=0; r<rows; r++)
 for(c=0; c<cols; c++)
 tot+=ar[r][c];
 return tot;
 }程序运行时很多错误,但如果把int ar[rows][cols]改成 int  ar【3】【4】这样具体的数据表达就可以了,二维数组作为函数参数传递时不可以这样用吗,rows,cols虽然是变量,但在函数调用时先传递这两个参数进来,不就相当于int ar[rows][cols]中的值是常量了么,还是说高版本的c语言软件才可以这样定义使用。(我的版本是c++6.0).是否可以用指针来代替int ar[rows][cols]。
 
 
 | 
 |