2

My intention is to output a list with offsets of all struct members from a typedef struct.

In my case this struct is stored in an external EEPROM which can be accessed bytewise via an interface accessible via a serial connection.

To access the EEPROM contents, I need to know the offsets of the struct members.

How can I tell the compiler to perform the offsetof task on every struct member?

Juri Robl
  • 5,614
  • 2
  • 29
  • 46

1 Answers1

1

The C standard, and most C implementations, do not provide a way to automatically list the offsets of structure members or to iterate through structure members. You must either manually list the structure members or write your own software to parse the source code defining the structure (and to use the results of parsing to generate more code to display the offsets).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • would c++ provide the functionality to iterate through structure members? – user3110467 Dec 17 '13 at 09:45
  • #define offsetof(st, m) ((size_t)(&((st *)0)->m)) << this trick should work anyway. – Jekyll Dec 17 '13 at 09:50
  • 1
    @Jekyll: That gives the offset of one member (if it works). It does not iterate through members. The question seeks a way to automatically produce the offsets of **all** members. – Eric Postpischil Dec 17 '13 at 09:52
  • all members ? :O Gosh! @EricPostpischil i didn't read it properly. I translated "all" with "any member I wish" that made more sense to me :D – Jekyll Dec 17 '13 at 09:54
  • thank you very much! I'll have a further reading on "reflections" – user3110467 Dec 17 '13 at 10:19