通过c++函数传递多维数组

Passing Multidimenionall Arrays through C++ functiones

本文关键字:数组 c++ 函数 通过      更新时间:2023-10-16
 #include <iostream>
using namespace std;
int ROWS = 3;
int COLS = 4;

我如何解决这个c++多维数组问题?这个问题我已经研究了一段时间了,但我就是想不明白,非常感谢。

void fillScores(int [ROWS][COLS]);
int main() {
    int scores[ROWS][COLS] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
    fillScores(scores);
    return 0;
}
void fillScores(int newScores[ROWS][COLS]) {
    cout << newScores[1][1]<<endl;
}

你可以把数组包装在一个结构体中,然后在你需要的地方通过地址传递它。

struct ArrayWrapper
{
   int _arr[ROWS][COLS];
};