I have an understanding issue regarding a programming technique: using pointer to navigate into nested structures.
what I have (pseudo code):
typedef struct lookup_descriptor_s {
uint8_t LookupData[9];
unsigned LookupDataSize: 1;
} lookup_descriptor_t;
typedef struct descriptor_s {
lookup_descriptor_t *IdLookupList;
device_descriptor_t *DeviceList;
usage_descriptor_t *UsageList;
uint32_t *member;
uint32_t member2;
} descriptor_t;
typedef struct rf_mac_setup_s {
...some_members
struct descriptor_s *description_table;
int foo;
char foo2;
etc...
} rf_mac_setup_t;
Here is the executed code :
descriptor_t *p;
rf_mac_setup *rf_mac;
p = rf_mac->description_table;
for (uint8_t i = 0; i < some_size; i ) {
if (some_function(p)) {
return p;
}
p ;
}
My question is : when p is incremented the first time, at which member does it point? Does it point to rf_mac_setup->foo
or p->DeviceList
, or even p->IdLookupList->LookupDataSize
? How does this pointer will step forward with each incrementation? (okay, that's two questions). I am a little bit confused here on how this pointer will allow me to navigate into these structures. Note that I am programming with a specific board and I can't launch debug session (unfortunately!).
I tried to read the memory, but I fail in hard fault handlers. I tried to search, but the topics I found weren't answering my problem.
CodePudding user response:
descriptor *p; rf_mac_setup *rf_mac; p = rf_mac->description_table; for (uint8_t i = 0; i < some_size; i ) { if (some_function(p)) { return p; } p ; }
My question is : when p is incremented the first time, at which member does it point?
There is no reason to think that p
ever points to any member of *rf_mac
. It is initially set equal to rf_mac->description_table
, with the result that the two pointers point to the same object (supposing that the pointer is valid). But it is the pointer rf_mac->description_table
itself that is the member, not whatever object it points to at any given time.
When you increment p
, it points to the next element of the array of struct descriptor_s
in which the previously pointed-to object resides, or immediately past the end of that array, where an object that is not part of a declared array is treated as an array of length 1. That will not be any of the structure members that have been shown.
CodePudding user response:
Okay, I think I understood, thanks to all the good explanations: If we take a trivial example:
typedef struct struct1_s {
uint8_t table[8];
int foo;
char foo2;
} struct1_t;
struct_t struct_var;
struct_t *p;
p = &struct_var; // p points to the first element of struct_var which is table
p ; // now, p points to foo
Do I have it right for now?
Now, with nested structures :
typedef struct struct1_s {
struct_2_t struct_2;
struct_3_t struct_3;
char foo2;
} struct1_t;
struct_t struct_var;
struct_t *p;
p = &struct_var; // p points to the first element of struct_var which is struct_2
p ; // now, p points to struct_3 even if number of elements and size of struct_2 and struct_3 are differents
Am I understanding the mechanism correctly? Thank you again for all your explanations.