变长数组
- C/C++ code
#include<stdio.h>#define ROWS 3#define COLS 4int 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} //2唯数组 }; 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])//通常函数定义都是sum(int ar[][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;}d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(4) : error C2057: expected constant expression
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(4) : error C2466: cannot allocate an array of constant size 0
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(4) : error C2057: expected constant expression
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(4) : error C2466: cannot allocate an array of constant size 0
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(4) : error C2087: '<Unknown>' : missing subscript
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(19) : error C2057: expected constant expression
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(19) : error C2466: cannot allocate an array of constant size 0
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(19) : error C2057: expected constant expression
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(19) : error C2466: cannot allocate an array of constant size 0
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(19) : error C2087: '<Unknown>' : missing subscript
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(19) : error C2133: 'varr' : unknown size
d:\program files\vc6.0\myprojects\10_lizi_18\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
d:\program files\vc6.0\myprojects\10_lizi_18\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
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(26) : error C2143: syntax error : missing ')' before ';'
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(31) : error C2057: expected constant expression
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(31) : error C2466: cannot allocate an array of constant size 0
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(31) : error C2057: expected constant expression
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(31) : error C2466: cannot allocate an array of constant size 0
d:\program files\vc6.0\myprojects\10_lizi_18\1.cpp(31) : error C2087: '<Unknown>' : missing subscript
[解决办法]
C99支持变长数组--但很有限。LZ先试试 一维数组,先看看你的编译器对C99的支持程度,再试试二维的,我的经历是,变长数组只在一级调用层可以用(向下调用时传变长数组相关参数可以),但不能再向下传了。