How can I access structure fields by name at run time?

前端 未结 5 1059
生来不讨喜
生来不讨喜 2020-12-18 07:31

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!

5条回答
  •  执念已碎
    2020-12-18 07:44

    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.

提交回复
热议问题