[英]Passing a 2D array of unknown size into a function in C++
我正在尝试将2D数组输入到函数中。 我不知道该数组的行数或列数,它是通过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
。 看不见就很难说。
有关CImg的data
和其他成员函数的更多信息,请参见http://cimg.eu/reference/structcimg__library_1_1CImg.html 。
为什么不通过它作为参考?
summation = SUM(prettypicture);
float SUM(const CImg<unsinged char>& picture) {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.