Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 614
Mike Avatar asked Mar 14 '13 16:03

Mike


People also ask

How do I enable warnings in GCC?

GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.

Can we define empty structure?

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.

Can we define empty structure in C?

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 is the use of empty 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).


Video Answer


1 Answers

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

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

like image 90
ouah Avatar answered Oct 03 '22 08:10

ouah