Mike
Mike

Reputation: 49393

enable warnings for empty structure

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...

  1. Am I reading the spec correctly that this is not allowed in C? (and more surprisingly did Microsoft get it right?!)
  2. Is there an option that can be passed in to gcc to make it catch this issue?

Upvotes: 5

Views: 596

Answers (1)

ouah
ouah

Reputation: 145829

  1. Yes, a structure type with no member is not valid in C.

  2. -Werror -pedantic with gcc will stop the translation.

Upvotes: 7

Related Questions