Is there any way to loop through a struct with elements of different types in C?

后端 未结 4 1741
迷失自我
迷失自我 2020-11-29 21:45

my struct is some like this

typedef struct {
  type1 thing;
  type2 thing2;
  ...
  typeN thingN;
} my_struct 

how to enumerate struct chil

4条回答
  •  我在风中等你
    2020-11-29 22:16

    There is no safe way to enumerate a struct's members, unless the exact contents of the struct is known, and even in that case you have to be careful of things like struct alignment/padding.

    Depending on your problem, it might be safer to have an array of your struct.

提交回复
热议问题