conversion of 2D array to pointer-to-pointer(二维数组到指针的转换)
问题描述
Activity solution[a][b];
...
Activity **mother = solution;
我想将对象的二维数组转换为指针到指针.我该怎么做;
I want to convert 2D array of objects to pointer-to-pointer. How can I do this;
我在谷歌上搜索过.但是我只找到了一维数组示例.
I searched it on google. however I found only one dimension array example.
推荐答案
这里仅仅进行转换对您没有帮助.二维数组类型和指针类型之间没有任何类型的兼容性.这种转换毫无意义.
A mere conversion won't help you here. There's no compatibility of any kind between 2D array type and pointer-to-pointer type. Such conversion would make no sense.
如果你真的需要这样做,你必须引入一个额外的中间行索引"数组,它将弥合二维数组语义和指针到指针语义之间的差距
If you really really need to do that, you have to introduce an extra intermediate "row index" array, which will bridge the gap between 2D array semantics and pointer-to-pointer semantics
Activity solution[a][b];
Activity *solution_rows[a] = { solution[0], solution[1] /* and so on */ };
Activity **mother = solution_rows;
现在访问 mother[i][j]
将使您可以访问 solution[i][j]
.
Now accessing mother[i][j]
will give you access to solution[i][j]
.
这篇关于二维数组到指针的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:二维数组到指针的转换
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- Stroustrup 的 Simple_window.h 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 近似搜索的工作原理 2021-01-01