获得的C结构成员名单 [英] Get list of C structure members

查看:82
本文介绍了获得的C结构成员名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能得到一个结构作为字符的成员名单**

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

例如,这样的事情:

struct mystruct {
    int x;
    float y;
    char *z;
};

/* ... */

char **members = MAGIC(struct mystruct); /* {"x", "y", "z", NULL}. */

我也有兴趣在编译器相关的方法。是否有这样的事情吗?

I am also interested in compiler-dependent methods. Is there such a thing ?

感谢您的时间。

推荐答案

有绝对的标准方法。

如果你愿意编译code两次,你可以有一个preprocessor界定包裹codePATH仅用于第二遍其内容由该公司生产的编译单元调试信息启用先通过获得成员名称。你也可以分析源$ C ​​$ C来获得在运行时列表中。

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.

最后,你可以使用preprocessor宏来定义的结构,并有宏也排放到另一个变量中的每个结构成员的条目,有效地保持同步两个不到直接有关的项目。

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.

这篇关于获得的C结构成员名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆