c++ - 将未知大小的二维数组传递给 C++ 中的函数

标签 c++ arrays function pointers cimg

我正在尝试将二维数组输入到函数中。我不知道这个数组的行数或列数,它是通过 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/

相关文章:

C++ Performance 从磁盘写入和读取

c++ - 检查 vector<vector<queue<msg>>> 中的 vector 'space' 是否为空

c++ - 检查正确的输入会导致无限循环

c - int a[9] 和 a[3][3] 的区别

arrays - 有没有更好的方法来创建数组的索引?

javascript - 从 json 中检索唯一值

swift - 使用默认值参数覆盖函数时,如何保持默认值与父类(super class)相同?

javascript - 是否可以用自定义函数覆盖 window.location 函数?

C++ 编译器错误 : ISO C++ forbids declaration of ‘set’ with no type

c++ - 使用指向成员函数的指针的问题