c++ - 存储大小未知

标签 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;
}

我在 main 函数中不断收到““图像”的存储大小未知”,但不确定为什么。

最佳答案

除非使用初始化列表,否则在声明数组时不能执行 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

在指定函数的形参时,可以保留二维数组的行维度,即

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

关于c++ - 存储大小未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36668615/

相关文章:

arrays - 你将如何在 Swift 中创建一个具有 n 维的多维数组?

c++ - 在堆或堆栈中创建构造函数有什么区别?

c++ - libssh2 和 C++

c++ - 使用 CPLEX 12.8 时的 GCC/G++ 警告

python - 将 C++ opencv 移植到 Python - 数组问题

c++ - 如何将多维数组传递给C++中的头文件

c++ - 调试程序以列出用户选择的 emirp 数的数量(素数也反素数)

c# - 多项式除法

python - 如何计算 NumPy bool 数组中真实元素的数量

java - 填充二维数组的最后 n 个元素