C++:如何获取指向二维动态数组的指针?

标签 c++ arrays pointers

我看了this excellent answer但无法弄清楚如何将其应用于此片段:

//this is in the .hpp file
std::atomic<int> size = 10;
std::recursive_mutex *locks[2];

//in some function of the class
//it's important that the 2nd array dimension is dynamic
the_lock[0] = new std::recursive_mutex[size]; 
the_lock[1] = new std::recursive_mutex[size];
std::recursive_mutex (*locks_2)[2][size] = &locks;

作业给我

error: cannot convert ‘std::recursive_mutex* (*)[2]’ to ‘std::recursive_mutex (*)
[2][(((sizetype)(((ssizetype)((**here be long type information, since I'm using
templates a lot**, long unsigned int, std::less<long unsigned int>          
>::size.std::atomic<long unsigned 
int>::<anonymous>.std::__atomic_base<_IntTp>::operator 
std::__atomic_base<_IntTp>::__int_type<long unsigned int>()) + -1)) + 1)]’ in 
initialization

如何获得指向“锁”的指针?

最佳答案

错误信息其实是免费赠送解决方案:

std::recursive_mutex * (*locks_2)[2] = &locks;

关于C++:如何获取指向二维动态数组的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17458881/

相关文章:

c++ - C++0x 中 std::make_tuple 的问题

c++ - 确定未定义函数的参数类型

Java:计算数组中的每个数字

java - 组件状态改变时的 Action

c - 关于 C 中指针的问题?

访问结构指针的正确方法

c++ - 来自 HHVM 构建的内部编译器错误

java - 更喜欢非成员非友元函数......在 Java 中?

将单词生成为 C、C++ 代码的 PHP 代码

c - 按特定顺序对 argv 进行排序