5

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?
Mike
  • 47,263
  • 29
  • 113
  • 177
  • 1
    See http://stackoverflow.com/questions/755305/empty-structure-in-c?rq=1 – Nick Mar 14 '13 at 16:50
  • 1
    @Nick - Not really the same. A) that question was about getting *rid* of the warning, I *want to* see the warning. B) the subquestion to the one you linked to was about adding elements and keeping the size 0, I don't care about whatever he's talking about C) that was asked in 2009 prior to the C11 standard comming out so there could very well be new information. – Mike Mar 14 '13 at 17:07

1 Answers1

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

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

ouah
  • 142,963
  • 15
  • 272
  • 331
  • +1 There's my warning! Why the heck does `gcc` let this go with the standard setup if it's not valid C? – Mike Mar 14 '13 at 17:02
  • 4
    Because `gcc` tries not to break a lot of code that depends on extensions in some compilers. By default `gcc` is very forgiving. – ouah Mar 14 '13 at 17:16