The C faqs explain it in a way, here is the link.
But I can\'t understand it, Somebody can explain it for me? Or give me another way?
Thanks so much!
If you have your binary compiled with debug information, you can use it to lookup names at runtime. For example gcc
(typically) produces debug info in DWARF format, and you can use libdwarf to process it.
In case of DWARF you can find your field in DW_TAG_member
node, DW_AT_data_member_location
attribute will give you the field's offset, same as you get from offsetof()
at compile time.