5
#include <stdio.h>

struct Header
{
    unsigned long long int alignment;
};

int main(void)
{
    struct Header header;  // note: we can loose the 'struct' in C++
    struct Header* pheader = &header;

    return 0;
}

The program above compiles perfectly in both C and C++.

But when i change the Header struct to:

struct {
    unsigned long long int alignment;
} Header;

it fails with the following message in C: error: storage size of ‘Header’ isn’t known

and in C++: error: aggregate ‘main()::Header header’ has incomplete type and cannot be defined struct Header header;

A similar structure is used in the implementation of the Storage Allocator in the C programming Language book by K&R. I thought it was the same thing, but I learn that it isn't. I have since seen in other places as well. I am of course more familiar with the first version. What does the second one mean and why does it even exist? What is the difference?

KeyC0de
  • 4,728
  • 8
  • 44
  • 68
  • Missing a `typedef` in the [case where it's a trailing name](https://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c)..? – user2864740 Oct 25 '18 at 02:19
  • Possible duplicate of [Are "anonymous structs" standard? And, really, what \*are\* they?](https://stackoverflow.com/questions/14248044/are-anonymous-structs-standard-and-really-what-are-they) – Jans Oct 25 '18 at 02:31

3 Answers3

6

struct Header {}; introduces a struct type named Header.

typedef struct {} Header; introduces an anonymous struct type, and an alias Header for that anonymous type.

struct {} Header; introduces both an anonymous struct type, and a variable named Header of the anonymous type.

When there is no type named Header (the last case), struct Header header; introduces a struct type named Header without a body, then tries to create a variable header of that type.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • @Nik-Lz In C++, `decltype(Header)` can be used to extract the (anonymous) type of `Header`. In C, I don't believe there is a way to name the type of `Header` and use it on anything else. – Yakk - Adam Nevraumont Oct 25 '18 at 18:21
1

When you are compiling below portion

struct {
    unsigned long long int alignment;
} Header;

struct has no tag, it's called anonymous structure type. while doing

struct Header header;

compiler produces an error like

Header header’ has incomplete type and cannot be defined struct Header header

Hence in these cases it's better to typedef the struct. For e.g

typedef struct {
    unsigned long long int alignment;
} Header;
Achal
  • 11,821
  • 2
  • 15
  • 37
1

In simple terms, the compiler sees "Header" and doesn't have a previous or built in data type of "Header" so it doesn't know what it is. When you use

typedef struct header {
    ....
} Header;

The compiler can say hey the user told me a defined data type and now I can have something to reference later on in the code.

Caleb lee
  • 43
  • 7