存储大小未知

时间:2016-04-16 19:19:56

标签: c++ arrays multidimensional-array

const int MAXWIDTH = 512;
const int MAXHEIGHT = 512;

void readImage(int image[][MAXHEIGHT], int &width, int &height)

....

void writeImage(int image[][MAXHEIGHT], int width, int height)

....

int main ()
{
 int image[][MAXHEIGHT], width, height;

 readImage (image, &width, &height);
 writeImage (image, width, height);
 return 0;
}

我在主要功能中不断获得“存储大小”图像“未知”,并且不确定原因。

1 个答案:

答案 0 :(得分:1)

除非您使用初始化列表,否则在声明数组时无法int image[][MAXHEIGHT];。即

int image[][5]={ {1,2,3,4,5},{6,7,8,9,10} };//is OK and the compiler get the row size from your initializer list and creates image[2][5]

int image[][5];//this is not OK because the compiler can't know the size of the row when creating the arrray

int image[MAXWIDTH][MAXHEIGHT];//OK because you specified both dimensions

指定函数的形式参数时,可以保留二维数组的行维度.i.e

void somefunction(image[][MAXWIDTH])//is OK