Search code examples
cstructtypedef

What's the difference between struct and typedef struct?


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;

Solution

  • 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.