I am wondering how to access the nth element of a multi dimensional array using a pointer to that array.
Assume I have the following
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
}
}
}
}
You have to dereference the pointer first:
(*Array_ptr)[i][j][k].X = 5;
Edit:
if ( (*Array_ptr)[i][j][k].ptr->asdf)
crashes
That's because .ptr
is uninitialzied.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.