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]
.
这篇关于二维数组到指针的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:二维数组到指针的转换


基础教程推荐
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01