代码编译不了#includestdio.h#define ROWS 3#define COLS 4int sum2d(int rows,int cols,int ar[rows][c
代码编译不了
#include<stdio.h>
#define ROWS 3
#define COLS 4
int sum2d(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("3x5 array\n");
printf("sum of all elements=%d\n",sum2d(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 sum2d(int rows,int cols,int ar[rows][cols])
{
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;
}
F:\1.cpp(4) : error C2057: expected constant expression
F:\1.cpp(4) : error C2466: cannot allocate an array of constant size 0
F:\1.cpp(4) : error C2057: expected constant expression
F:\1.cpp(4) : error C2466: cannot allocate an array of constant size 0
F:\1.cpp(4) : error C2087: '<Unknown>' : missing subscript
F:\1.cpp(19) : error C2057: expected constant expression
F:\1.cpp(19) : error C2466: cannot allocate an array of constant size 0
F:\1.cpp(19) : error C2057: expected constant expression
F:\1.cpp(19) : error C2466: cannot allocate an array of constant size 0
F:\1.cpp(19) : error C2087: '<Unknown>' : missing subscript
F:\1.cpp(19) : error C2133: 'varr' : unknown size
F:\1.cpp(24) : error C2664: 'sum2d' : cannot convert parameter 3 from 'int [3][4]' to 'int [][1]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
F:\1.cpp(26) : error C2664: 'sum2d' : cannot convert parameter 3 from 'int [2][6]' to 'int [][1]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
F:\1.cpp(31) : error C2057: expected constant expression
F:\1.cpp(31) : error C2466: cannot allocate an array of constant size 0
F:\1.cpp(31) : error C2057: expected constant expression
F:\1.cpp(31) : error C2466: cannot allocate an array of constant size 0
F:\1.cpp(31) : error C2087: '<Unknown>' : missing subscript
用的是vc++6.0
C
[解决办法]
VC6不能用变量作数组大小吧。。