我正在尝试将二维数组输入到函数中。我不知道这个数组的行数或列数,它是通过 CImg 加载到 C++ 中的。这是我的:
// Main function:
int main()
{
int rows, columns;
float summation;
CImg<unsigned char> prettypicture("prettypicture.pgm");
rows = prettypicture.height();
columns = prettypicture.width();
summation = SUM(prettypicture[][], rows, columns);
}
// Summation function:
float SUM(int **picture, int rows, int column)
{
... // there is some code here but I don think this is important.
}
我想将数组传递给求和函数,我知道我应该以某种方式使用指针,但我不确定如何执行此操作。任何帮助将不胜感激。
谢谢
(抱歉我是菜鸟)
最佳答案
试试这个:
summation = SUM(prettypicture.data(), rows, columns);
并使您的 SUM 函数看起来像这样:
float SUM(char* picture, int rows, int column) ...
您需要传入data
(如果您想要一个指向数据的指针),因为这是CImg 提供的。它是指向字符的指针,因为那是您拥有的那种 CImg;它是 char*
,而不是 char**
,因为这是数据提供的。
你没有向我们展示 SUM 函数的内部,所以我想知道你是否可以传入 CImg 而不仅仅是它的数据,并调用成员函数 atXY
一个位置。很难说没有看到更多。
有关 data
和 CImg 其他成员函数的更多信息,请参阅 http://cimg.eu/reference/structcimg__library_1_1CImg.html .
关于c++ - 将未知大小的二维数组传递给 C++ 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35559928/