offsetof at compile time

前端 未结 3 1861
情歌与酒
情歌与酒 2020-12-09 13:05

Is there a way of finding the offset of a member of a structure at compile-time? I wish to create a constant containing the offset of a structure member. In the following co

3条回答
  •  执念已碎
    2020-12-09 13:33

    It compiles without warning here with g++ 4, after I add the proper #includes.

    Are you #including stddef.h? offsetof() is a macro, not a built-in keyword in C.

    If that doesn't fix it, try making the constant static, to confine it to the module. That might make the compiler happy.

提交回复
热议问题