I have a question about empty structures in C. As far as I can tell from reading the standards, it seems that they are not allowed:
6.2.5-20
— 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.
So, not surprisingly when attempting to compile something like:
struct foo
{
};
In MS VS, there's some error thrown:
error C2016: C requires that a struct or union has at least one member
However, when compiling the same code with gcc -Wall -Werror
there are no errors seen. So...
gcc
to make it catch this issue? GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.
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.
Yes, it is allowed in C programming language that we can declare a structure without any member and in that case the size of the structure with no members will be 0 (Zero). It will be a Zero size structure.
What can I use an empty struct for, if it has no fields? Basically an empty struct can be used every time you are only interested in a property of a data structure rather than its value (this will be more clear in the practical examples).
Yes, a structure type with no member is not valid in C.
-Werror -pedantic
with gcc
will stop the translation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With