我写了下面的代码,但是显示错误
use of parameter outside function body before ‘]’ token
代码是
#include <iostream>
using namespace std;
int n=10;
void a(int s[n][n])
{
cout<<"1";
}
int main()
{
int s[n][n]={0};
a(s);
}
我正在尝试使用全局变量传递可变大小的多维数组。我不想在此使用 vector 。
最佳答案
首先,C++ 没有变长数组,所以用 int s[n][n]={0};
代替你应该使用
std::vector<std::vector<int>> s(10,std::vector<int>(10));
其次如何将二维数组传递给函数,
void a(std::vector<int> **s,int rows, int cols){
cout<<"1";
/* stuff with 2D array */
}
关于c++ - 在c++中的函数中传递可变大小的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49013656/