如何将二维数组指向函数的指针作为参数
How to put 2-dimensional Array's pointer to the function as the parameter
我正在用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
您需要用malloc
、calloc
或new
分配内存:
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);
相关文章:
- 为函数定义符号不明确的指针参数
- 构造函数 (C++) 中的 char 指针参数存在问题
- 按引用调用与按指针参数调用的差异 前递增和后递增
- 为什么具有指针参数的成员函数需要指向指针的指针?
- 通过引用函数传递指针参数是什么意思?
- 从double到double*的指针参数
- void 函数中的指针参数返回不一致的值
- 无法向上转换指针到指针参数
- 将数组动态分配到具有指针参数的函数中
- 模板函数指针参数与构造函数参数
- 使用通用函数指针参数化函数模板的简洁方法
- 如何为C++字符串分配空指针参数?
- C++带有指针参数的函数
- 指针参数的默认值
- 将常量添加到函数模板指针参数
- 如何传递unique_ptr<T>代替原始*输出*指针参数?
- 不带星号的函数指针参数
- 功能指针参数参数转换为const
- 将指针参数传递给双指针参数
- 如何调试指针参数是否通过函数修改