Structure in C

Let’s introduce a new topic today : Structure.

Before going through any of the basics, take a look at this example below:

Let’s say you are asked to store name and age of 3 footballers. With the knowledge you have at present, your approach will be something like this:
char name1[20] = “messi”;

int age1 = 37;

char name2[20] = “ronaldo”;

int age2 = 39;

char name3[20] = “neymar”;

int age3 = 32;

So, you need 2 * 3 = 6 variables. If you are asked to store same information for 20 different players, you need 2 * 20 = 40 variables

A better approach would be to have a collection of all related information (name and age) under a singe variable like this:

players player1 = { “messi”, 37};

players player2 = { “ronaldo”, 39};

players player3 = { “neymar”, 32};

So, how can we do this? Let’s brainstorm: we have used datatypes like int, char, float and data structures like array so far. An int will only store an integer value, and same goes for both char and float. An array will store a number of values of same datatype. 

But none of these will do our task. Why? Because we need to store a string (or char array) and an int under a single variable. None of the available built-in functionalities in C will let you do this task.

However, to do this you have to have a new user-defined datatype. To do this C has a special feature called Structure (or struct). So, let’s do this:

Take a look at this code, specially line 4 to line 7. 

In line 4, we are informing the compiler that we are going to create a new data-type named player by using the keyword struct. Our very own new data-type player can store values of 2 different datatypes : name as string (Line 5) and age as int (Line 6). Consider name and age as the members of player structure. And finally, don’t miss out the curly brackets and ending semicolon (Line 7) as these are syntax for C structure.

So, at this point we have a data type named player (in addition with other built-in data types like int, char, float). Now let’s declare some variables and initialize values.

Take a close look at line 10. Here, on the left hand side of the equal operator (=), we have struct player player1. Now consider this struct player player1 is equivalent to int a


struct player is equivalent to int (you have to use struct before player as player is not a built-in data type, rather a user defined data type)  

And, player1 is equivalent to a (i.e., the player1 is a player type variable)

On the right hand side of equal operator, we have {“messi” , 37}. Why? Because, our player data type can store 2 different values of string and integer under a single variable name (here, player1 is that variable). And after executing line 10, our compiler will know = “messi”

player1.age = 37

In other words, the dot (.) is used to access members of a structure (the members are name and age in our case).

The same goes for Line 11 and 12.

Finally, line 14–16 print the values of player1, player2, player3 by accessing their members using the dot (.) operators.

I hope I have explained it well. Thank you.