Is there a way to determine a member offset at compile time?

后端 未结 3 500
没有蜡笔的小新
没有蜡笔的小新 2020-12-19 15:57

I\'m finding I\'m spending a lot of time trying to determine member offsets of structures while debugging. I was wondering if there was a quick way to determine the offset

3条回答
  •  [愿得一人]
    2020-12-19 16:30

    This should do:

    #define OFFSETOF(T, m) \
      (size_t) (((char *) &(((T*) NULL)->m)) - ((char *) ((T*) NULL)))
    

    Call it like this:

    size_t off = OFFSETOF(struct s, c);
    

提交回复
热议问题