C/C++ Structure offset

前端 未结 3 1358
自闭症患者
自闭症患者 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:33

    Right, use the offsetof macro, which (at least with GNU CC) is available to both C and C++ code:

    offsetof(struct mstct, myfield2)
    

提交回复
热议问题