C/C++ Structure offset

前端 未结 3 1374
自闭症患者
自闭症患者 2020-12-14 15:11

I\'m looking for a piece of code that can tell me the offset of a field within a structure without allocating an instance of the structure.

IE: given



        
3条回答
  •  生来不讨喜
    2020-12-14 15:22

    How about the standard offsetof() macro (in stddef.h)?

    Edit: for people who might not have the offsetof() macro available for some reason, you can get the effect using something like:

    #define OFFSETOF(type, field)    ((unsigned long) &(((type *) 0)->field))
    

提交回复
热议问题