Empty structure in C

后端 未结 7 995
臣服心动
臣服心动 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 14:53

    if you just need the struct symbol for casting and function arguments then just:

    typedef struct _Interface Interface;
    

    this will create the symbol for an opaque type.

    0 讨论(0)
  • 2020-12-01 14:54

    C99 standard is somewhat ambiguous on this, but seems to say that an empty struct should have non-zero size.

    6.2.6.1 Except for bit-fields, objects are composed of contiguous sequences of one or more bytes, the number, order, and encoding of which are either explicitly specified or implementation-defined.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-01 15:01

    Technically this isn't even valid C.

    TrayMan was a little off in his analysis, yes 6.2.6.1 says:

    Except for bit-fields, objects are composed of contiguous sequences of one or more bytes, the number, order, and encoding of which are either explicitly specified or implementation-defined.

    but tie that with 6.2.5-20, which says:

    — A structure type describes a sequentially allocated nonempty set of member objects (and, in certain circumstances, an incomplete array), each of which has an optionally specified name and possibly distinct type.

    and now you can conclude that structures are going to be one or more bytes because they can't be empty. Your code is giving you a warning, while the same code will actually fail to compile on Microsoft's Visual Studio with an error:

    error C2016: C requires that a struct or union has at least one member

    So the short answer is no, there isn't a portable way to avoid this warning, because it's telling you you're violating the C standards. You'll have to use a compiler specific extension to suppress it.

    0 讨论(0)
  • 2020-12-01 15:02

    Is it possible to add a member and keep the sizeof the struct zero?

    Nope. FWIW, C++ allows empty structs but the sizeof() is always non-zero for an empty struct.

    Any other solution?

    Not any easy ones. It's worth noting that empty structs are only somewhat supported in C and disallowed in C99.

    Empty structs are supported in C++ but different compilers implement them with varying results (for sizeof and struct offsets), especially once you start throwing inheritance into the mix.

    0 讨论(0)
  • 2020-12-01 15:05
    struct zero_information { int:0; };
    

    The above code snippet will yield a non-zero value from sizeof(struct zero_information), but it might help you get what you looking for as 100% of all the storage that is allocated for it, is padding (only accessible through hacks, although I don't remember from the top of my head if acceding the padding is undefined behavior).

    0 讨论(0)
提交回复
热议问题