如何在 C 中分配二维指针数组
我正在尝试使指针指向二维指针数组。语法是什么以及如何访问元素?
I'm trying to make a pointer point to a 2D array of pointers. What is the syntax and how would I access elements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
根据法律规定,具体操作方法如下:
小心地以正确的顺序单独删除包含的指针、行数组和列数组。
然而,在 C++ 中,您更频繁地创建一个类,在内部管理一维指针数组并重载函数调用运算符以提供二维索引。这样你就真正拥有了一个连续的指针数组,而不是一个指针数组的数组。
By the letter of the law, here's how to do it:
Be careful to delete the contained pointers, the row arrays, and the column array all separately and in the correct order.
However, more frequently in C++ you'd create a class that internally managed a 1D array of pointers and overload the function call operator to provide 2D indexing. That way you're really have a contiguous array of pointers, rather than an array of arrays of pointers.
这取决于。它可以很简单:
如果您希望在运行时动态大小,您需要做一些工作。
但是 Boost 可以满足您的需求:
如果您对 锯齿状数组可以动态增长:
注意:在上述所有内容中。我假设数组不拥有指针。因此,它没有对其包含的指针进行任何管理(尽管为了简洁起见,我在示例中使用了 new int() )。要正确进行内存管理,您需要做更多的工作。
It depends. It can be as simple as:
If you want dynamic sizes at runtime you need to do some work.
But Boost has you covered:
If you are fine with jagged arrays that can grow dynamically:
Note: In all the above. I assume that the array does not own the pointer. Thus it has not been doing any management on the pointers it contains (though for brevity I have been using new int() in the examples). To do memory management correctly you need to do some more work.
这就是创建真正的(内存中连续的)多维数组的方法。
但要意识到,一旦将多维数组转换为这样的指针,您就失去了自动索引它的能力。您必须手动执行索引的多维部分:
That's how you make a true (contiguous in memory) multidimensional array.
But realize that once you cast a multidimensional array to a pointer like that, you lose the ability to index it automatically. You would have to do the multidimensional part of the indexing manually:
你可以尝试 Boost::MultiArray。
请查看此页面了解详细信息。
You could try Boost::MultiArray.
Check out this page for details.
:)
我在我写的一段代码中曾经有过这些。
当第一个 bug 泄露出去时,我成了团队的笑柄。最重要的是,我们使用匈牙利表示法,导致像
papChannel
这样的名称 - 指向指针数组的指针......这不太好。最好使用 typedef 来定义“行列”,反之亦然。也使索引更加清晰。
:)
I had these once in a piece of code I wrote.
I was the laughing stock of the team when the first bugs leaked out. On top of that we use Hungarian notation, leading to a name like
papChannel
- a pointer to an array of pointers...It's not nice. It's nicer to use typedefs to define a 'row of columns' or vice versa. Makes indexing more clear, too.
您可以定义向量的向量:
然后创建一个从 my_pointer2D 派生的类,例如:
You can define a vector of vectors:
Than create a class derived from my_pointer2D, like:
我更喜欢使用 () 运算符。造成这种情况的原因有很多(C++ 常见问题解答 13.10) 。如果您愿意,请将内部表示更改为
std::vector
:您可以像这样使用它:
I prefer to use the () operator. There are lots of reasons for this (C++ FAQs 13.10). Change the internal representation to a
std::vector
if you like:You can use it like this:
看我的代码。它适用于我的 FC9 x86_64 系统:
Effo UPD:对“这不是一个指向数组的指针数组吗?”的回应,添加了指针数组的示例,非常简单:
我是否仍然误解了你的问题?
你甚至可以
它也适用于我的 FC9 x86_64 系统。
See my code. It works on my FC9 x86_64 system:
Effo UPD: a response to "Isn't that an array of pointers to arrays?", adding the example of array of pointers, very simple:
Did I still misunderstand your question?
And you could even
It also works on my FC9 x86_64 system.
如果您需要二维数组的大小是动态的(即运行时已知的尺寸),您可以使用专门的库。
像Boost.MultiArray(其他答案),或Multi:
https://godbolt.org/z/G6WrP9vKW
If you need the 2D array to be dynamic in size (i.e. dimensions known at runtime) you can use a specialized library for that.
Like Boost.MultiArray (other answer), or Multi:
https://godbolt.org/z/G6WrP9vKW