[英]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
未初始化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.