Get list of C structure members

前端 未结 6 1782
盖世英雄少女心
盖世英雄少女心 2020-12-09 18:07

Is it possible to get the list of members of a structure as a char ** ?

For example, something like this:

struct mystruct {
    int x;
          


        
6条回答
  •  天命终不由人
    2020-12-09 19:02

    There's definitely no standard way.

    If you're willing to compile the code twice, you could have a preprocessor-define-wrapped codepath only enabled for the second pass which reads debugging information from the compilation units produced by the first pass to get the member names. You could also analyze the source code to get the list at run time.

    Finally, you could use preprocessor macros to define the struct and have the macros also emit an entry in another variable for each struct member, effectively keeping two not-directly-related items in sync.

提交回复
热议问题