我一直在尝试将未知大小的多维数组传递给函数,但到目前为止还没有成功,当数组被声明时,它的维度是变量:
double a[b][b];
据我所知,我需要在声明函数时给出b的值,a可以是未知的。我尝试将 b 声明为全局变量,但它接着说它必须是常量。
即:
int b;
double myfunction(array[][b])
{
}
int main()
{
int a;
double c;
double myarray[a][b];
c=myfunction(myarray);
return 0;
}
有什么办法让它起作用吗?
最佳答案
按值传递:
double myfunction(double (*array)[b]) // you still need to tell b
通过 ref :
double myfunction(int (&myarray)[a][b]); // you still need to tell a and b
模板方式:
template<int a, int b> double myfunction(int (&myarray)[a][b]); // auto deduction
关于c++ - 将多维数组传递给函数(C++)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11655326/