Finding the address of structure knowing the address of one of it's members in C
There's an old trick in C that allows you to #define a macro that does that. The trick goes:


Say your struct type is called struc_t. Create a pointer to one these and point it to any address:

struc_t *base_pointer = (struc_t*) 0;

Say the member whose address you know is struc_t.member; then you just get the address of that:

char *elem_pointer = &amp;(base_pointer-&gt;member);

Then, you get the offset by a funky mathematical operation called subtraction:

size_t offset = (elem_pointer-base_pointer);

Now, you can subtract offset from your element's address. Done!




If your processor's toolchain is halfway decent, it'll support the offsetof macro. (In fact, that's C89, see man offsetoff).