如何将二维数组指向函数的指针作为参数

How to put 2-dimensional Array's pointer to the function as the parameter

本文关键字:指针 参数 函数 二维数组      更新时间:2023-10-16

我正在用c++解决一个算法问题。

我想通过init动态输入,把每个维度大小不同的二维数组的指针。

我编码的函数如下:内容(函数的函数)没有任何意义。

int cal(int **arr){
int test = arr[0][0];
return 0;
}

和这个函数的结果

int arrayD[totalGroupCount][totalBeadCount];
int a = cal(arrayD);

它只是说"No Matching function call for 'cal'"

我确实声明了函数'cal'。

我用了很多不同的符号

int cal(int *arr[]){
int test = arr[0][0];
return 0;
}

但它说的都是我。

我已经搜索了这个问题,但我得到的答案也犯了同样的错误(我完全不明白他们是怎么做到的)

当你使用c++时,std::vector< vector<int > >有更好的解决方案

int cal(std::vector<std::vector<int> > arr)
{
    int test = arr[0][0];
    return 0;
}
并调用函数
std::vector<std::vector<int> >arrayD (totalGroupCount, std::vector<int>(totalBeadCount));
int a = cal(arrayD);

也可以使用push_back()函数动态添加元素到vector

您需要用malloccallocnew分配内存:

long a;
int **pt; // a pointer to pointer to int
pt=new int*[rows]; // allocate memory for pointers,
// not for ints
for (a=0;a<rows;++a) pt[a]=new int[columns]; // here you're allocating
// memory for the actual data

这将创建一个类似于pt[rows][columns]的数组。

然后像这样传递pt:

int Func(int **data) {
//do something
return //something
}
Func(pt);