1

What is the difference between:

typedef struct
{

} hello;

And:

struct hello
{

};

Sorry if it is a stupid question but i cannot understand what the difference is...

Erik W
  • 2,590
  • 4
  • 20
  • 33

1 Answers1

3

typedef is a keyword meaning "type definition". It is not part of the struct. In your first example, it makes hello to be a new type which is struct { ... }

The first creates a type; the second declares a struct named hello.

The difference is that the first creates a new type. The second only declares a struct. The difference is subtle, but in C, you cannot reuse a struct without the struct keyword: (In C++, the scope rules are different.)

To use the type, write something like this:

 typedef struct {
      int x, y;
 } hello;

 hello a, b, c;

This creates three variables all of type hello.

To use the struct, write this:

 struct xyz {
      int  z;
 };

 struct xyz  d, e, f;
wallyk
  • 56,922
  • 16
  • 83
  • 148