typedef
creates a new type, it has nothing to do with the structure. You can do so:
typedef int Inteiro
He’s creating a new guy named Inteiro
which will be based on the type int
.
Then you can do it too:
struct exemplo {
int x;
}
typedef struct exemplo Exemplo;
Which is the same as doing:
typedef struct exemplo {
int x;
} Exemplo;
I put in the Github for future reference.
So there exemplo
is the name of the structure, and Exemplo
is the name of the guy.
Note that I prefer to use type names starting with uppercase or Camelcase. There are those who like that of ALL CAPS, there are those who like everything tiny, there is no universal standard.
A structure need not have a name. Of course if it has no name or can only be used once, it must be defined in a typedef
which will then be used to create a data based on the anonymous structure.
I’m not particularly fond of naming the structure on a typedef
. Unless the type itself is used within the structure, it creates a problem, because the type cannot be used in a structure that defines it, the type does not yet exist when the structure is being defined. So in the last example it has the name because of this. The structure references itself before defining ELENCO
, then this name cannot be used, you need to use the struct no
.
Note that the structure name exists only in the scope of a struct
whereas the name of the type is global.