tags:

views:

884

answers:

4

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

warning: struct has no members

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

+1  A: 

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.

unwind
+4  A: 

In c the behaviour of an empty structure is compiler dependent versus c++ where it is part of the spec (explanations here)

C++
A class with an empty sequence of members and base class objects is an empty class. Complete objects and member subobjects of an empty class type shall have nonzero size.

in C it is rather more murky since the c99 standard has some language which implies that truly empty structures aren't allowed (see TrayMan's answer) but many compilers do allow it (e.g gcc).

Since this is compiler dependent it is unlikely that you will get truly portable code in this case. As such non portable ways to suppress the warning may be your best bet.

ShuggyCoUk
So there is no way to portably avoid this warning? (Else then having a compiler specific #pragma using #ifdef for example)
claferri
Moreover, for GCC Diagnostic Pragmas : "Also, while it is syntactically valid to put these pragmas anywhere in your sources, the only supported location for them is before any data or functions are defined." Which means to me I cannot suppress JUST this warning, am I right?
claferri
The question is about C, not C++.
@unwesen (I was pointing out the difference between them (as many people may assume that this is something where they are the same) the pragma stuff is relevant to the OP's problem. My answer is basically "it depends on the compiler sorry" with some specific compiler suggestions and info as to *why*
ShuggyCoUk
@claferrir - not if you want to keep the struct actually zero size (though this might not be the case portabbly *anyway*) sorry
ShuggyCoUk
+1  A: 

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.

TrayMan
+1  A: 

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.

Adisak