Empty structure in C

后端 未结 7 1037
臣服心动
臣服心动 2020-12-01 14:49

I have a structure with no members (for the moment) and I would like to know if it is possible to suppress the warning I get:

warning: struct has no members         


        
7条回答
  •  粉色の甜心
    2020-12-01 15:00

    If you're not requiring "too strict" adherence, you might get away with this:

    struct empty {
      char nothing[0];
    };
    

    This is a GCC extension, though.

    I was kind of hoping I'd be able to use the C99 feature called "flexible arrays", declared like this:

    struct empty99
    {
      char nothing[]; // This is a C99 "flexible array".
    };
    

    but that doesn't work; they require that there is at least one normal struct member first, they can't be the only member.

提交回复
热议问题