二维数组到指针的转换

2021-12-13 00:00:00 pointers static c++
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].

相关文章