指向多维数组的第 n 个元素的指针
Pointer to nth element of multi-dimensional array
我想知道如何使用指向该数组的指针访问该数组的第n个元素。
假设我有以下内容
struct Struct_B
{
bool asdf;
};
struct Struct_A
{
int X;
int Y;
Struct_B *ptr;
};
typedef Struct_A new_typedef[10][20][30];
new_typedef Array;
// Set values within Array, including correctly setting ptr variable
// Within debugger, Array_ptr contains the correct data, identical to Array, as it should.
new_typedef* Array_ptr = &Array;
for ( int i = 0; i < 10; i++) {
for ( int j = 0; j < 20; j++) {
for (int k = 0; k < 30; k++) {
// Crashes on this conditional'
// Within debugger, asdf is in some random location in memory, implying I am not accessing it correctly.
// Same problem exists if I attempt to access X or Y
if ( (*Array_ptr)[i][j][k].ptr->asdf) {
// Do stuff
}
}
}
}
您必须先取消引用指针:
(*Array_ptr)[i][j][k].X = 5;
编辑:
if ( (*Array_ptr)[i][j][k].ptr->asdf)
崩溃
那是因为.ptr
是未初始化的。
相关文章:
- 调整大小后指向元素值的指针unordered_map有效?
- 当该数组的索引中没有元素时,指针指向什么?
- 如何在基类指针向量的元素上应用重载的多态函数
- C++:添加新结构时,结构指针向量中的所有元素都会更新
- C++默认情况下,指针类型数组的元素是否保证初始化为 nullptr?
- 删除指针数组 (C++) 中的元素
- 从指向结构数组的指针中提取元素
- 指向数组基址的指针而不是指向第一个元素的指针有什么优点?
- 获取指向指针的指针元素 (c++)
- 经过最后一个数组元素末尾的指针是否应该等于超过整个数组末尾的指针?
- 存储指向动态数组元素的指针
- 通过作为指向 C++ 函数的指针传递来访问 std::array 元素的正确方法是什么?
- 如何使用指针C++将元素从2D数组复制到1D数组
- 如何将元素插入到标准::地图的共享指针中?
- C++通过别名指针以静默方式将错误的类型分配给数组元素
- 在删除之前更新一组元素指针中的元素
- 从 BST 中删除元素(指针错误)
- 避免强制转换容器的元素指针类型
- 填充一个无序的vector元素指针队列
- OpenCV元素指针如何存在于Obj-C类中