1

I'm learning c programming and I was wondering what's the difference between struct and typdef struct

Because from what I saw

struct Structure {
   char * name; 
   int number;
};

acts the same as

typedef struct {
   char * name;
   int number;
} Structure;
RedstonekPL
  • 13
  • 1
  • 4

2 Answers2

0

They are not the same.

The first creates a structure named struct Structure, and variables of this type are defined like this:

struct Structure myvar;

The second creates an anonymous structure and gives it the alias Structure. Variables of this type are defined like this:

Structure myvar;

Also, these two structs are not interchangeable because they have different names.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

In this declaration

struct Structure {
   char * name; 
   int number;
};

there is declared a type specifier struct Structure. Using it in declarations you have to write the keyword struct like

struct Structure s;

You may introduce a variable name Structure and it will not conflict with the the structure tag name because they are in different name spaces.

In this declaration

typedef struct {
   char * name;
   int number;
} Structure;

there is declared an unnamed structure for which there is introduced an alias Structure. You may not introduce the same name Structure for a variable in the same scope where the structure is defined.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335