typedef in structure

Do you remember one thing in my previous post? To create a structure variable we need to write like this:
struct player player1 = {“messi”, 37};

Here, the struct keyword at the beginning is required as we are using a user-defined data type player. However, this looks 

  • less efficient (as you need to write struct every time you create a new variable of type player)
  • Less realistic (as we are not used to writing struct when declaring an int variable for example)

To get rid of this struct keyword, you can use typedef. Look at the code below

Just focus on 2 things:

  • at line 20: instead of struct player, we are using typedef struct player
  • at line 23, before the ending semicolon (;), we have written player

These 2 changes will do the task and our compiler will treat player as struct player. As a result, at lines 26 to 28 we can declare player type variables without using struct keyword over and over again. Eventually, it is now more efficient and more realistic (it is now like declaring a variable of a built-in data type).

So, that’s it! Thank you.