Hello everyone, my name is Aditya and we are continuing with this C programming course.
In the last video, we discussed the structure.
We saw how we can make the structure and what exactly is this structure? We saw that the way we used to use the variable of one data type, in the same way we can make our own data type with the help of a structure and whatever thing we want in that, we can tweak it as per that and we can modify it.
We can include all those things in it and we can use it as a data type.
So, in this video we will see about nested structures.
We will see how the two structures are with us, how we can make use of the first structure as a structure member in another structure.
This is what we are going to learn and see in this particular video.
So, we will start the nested structure as a topic and we will understand here what exactly we will be doing.
We can use a structure variable as a member for another structure.
Now, here you will understand this definition with an example, suppose we have to store some details of the employees and we'll be making structures for that.
Why? Because in the employee's detail comes employee's name, his surname, his age we can count, mobile number, contact details and address as well.
So, all these things related to one employee, we have to store these things through our program.
And here there is one more thing that we have to see here, the address of any employee, even that address will be divided in different parts like house or flat number, along with that building or society’s name, then our local neighbourhood’s name, then the area name and city name.
So, in this way, the address of any employee will be divided in these different parts.
So, what do we have to do here? We will be making one structure first of all for our address.
What would be the members in it? First would be the house number, then would be the society's name.
Then, after that, the neighbourhood or the city name, in this way we will be making different members to complete the entire address.
So, this was about one structure that we will make for the address.
Now the other structure which is there, that will be the main structure and what will come in this main structure? All the details of the employee will come here like the employee’s name, his contact details, his age we can take, we can take his email id and along with that, we will also be taking his address.
So, this would be the main structure where all the information related to the employees are there.
And then we have made another normal structure in which we are making different parts for the address, we are making different structure members for the address.
Now what do we expect? We want that over address structure, I want to use this address structure, in my main structure as an address member.
Will I be able to do it here? So, this is the thing that we are seeing here that it is possible that you can use one structure through its variable in some other structure as a member.
Like there was the situation whether you made a different structure for the address and a different structure for the employee’s details but the address structure, you can use it as a member in another or the main structure.
And you can input all the values and data over there.
This entire process is called nested structures, which means in one structure, another structure can be made.
This concept we will be discussing over here.
So, we will quickly move ahead and we will see practically by using all these concepts.
How we can make one structure in another structure and we use it.
We will quickly go into our text editor and we will see one program there, in which basically we are making one structure.
Similar to what we discussed now.
There will be some information related to the employees in which the main structure will remain as it is.
But inside that when we will be storing the employee's address, there the role of another structure will come and this another structure we will be using here as a member.
How will we be using it? Come let's see.
First of all, in our program we know that we will start with our preprocessor command by importing the stdio.h file.
Apart from this file, we can import many other files.
We will be discussing that in the coming videos.
But for now, the functions that are used in the program or the requirements which are there are fulfilled by the stdio.h file.
That's why there is no need to input any other file over here.
So, we will go on to the next one.
You can see here that we have made a structure named address over here.
Now, how do we know that we have made a structure here? Since we have written here the struct keyword and in front of it, we have written one name which is an address and this address is the name of our structure.
Now we will come inside the address structure.
You can see here that there are three main members of our address structure.
The first member is “h n o” which denotes the house number.
In any address, house number or a flat number is obviously there.
So even here we are making “h n o”, one variable in which the house number will come and this would in integer values.
After that, you can see that the second member is a string and here basically we will be storing the city’s name.
So, its size has given us 20 and this is the character type.
That's the reason why we are calling it a string.
Now we will move ahead and see the third member, the third member is to store the PIN code.
In every address PIN code is extremely important.
With this, we get the information about the state and city.
So, we will store the PIN code as well.
Even this will be stored in the variable of integer data type.
So, in this way, we have made the address structure, where we have created three members.
So, here the first structure is over.
We are going ahead in our program.
We will see ahead that we have made one more structure, which is called employee.
What exactly is this employee structure? It stores the main data of our employees.
Now we will see how it stores the main data.
It's a member, the very first member, what is it basically doing? It is storing the employee’s name.
It is storing the name as a string.
Along with that if you see the second member, here there is a slight difference like we were making the members earlier, this member is slightly different which you can see.
So, what exactly is happening here? Let's see.
Here you will see we have written struct, address and then we have written add.
We all know that when we make any structure.
After making it we can use it as a data type and make different variables and through those variables according to the structure, we take the inputs of the values.
So even here we have made a variable for our structure.
But what is this structure? This is our address structure which we had we had made first on the top.
So, this struct address is telling us that the address structure we have made this type of variable.
A d d means add.
Even if this is a member, of what? Of the employee structure.
What we did is we made the variable of some other structure and we put it as a member in some other structure.
So, this was about a nested structure that shows how we can use one structure and we can utilise all its members in some other structure.
So, this is about both our structures.
Here we have made the variable of that type in the employee structure.
Here both the structure gets over, now we will come ahead and see our main function, which is the main part of our code.
As soon as we come ahead, you can see that we have made an employee structure type variable, with the name “emp.”
First, we had made an address structure variable, which we will be passing as a member in the employee structure.
But we have made another employee structure type variable, which is called EMP.
When I have to access any value in the employee structure, if I have to store it through it.
What I will be doing is using the EMP variable.
Here we will go ahead and we have made use of the printf statement, which is telling us that we have to add employee information here.
We have to write it, so entering employee information is what the printf statement is saying.
Now the printf has come, so after that we have to get scanf as well if we have to take some input.
So, that is why we have also got a scanf statement here.
Now there is a need to pay attention here.
Since there is scanf, we will be taking little inputs over here.
Since there is scanf, we are taking input from here.
So, what all are the inputs we are taking, what is it dependent on? How many format specifiers have we given over here? Now we have given 4 format specifiers over here.
You can see that; the format specifier is %s.
Along with that second %d and third percent is s again and %d is fourth.
Which means the first variable that we are using would be of extreme type, along with that the second one would be using integer.
Third would be again using string and the fourth one, again integer.
So, this is clear over here.
Now we will go to the first who is accepting the string.
So, you can see that the emp named variable that we had made here, through that we are accessing the name, we are taking the name input for one employee and that’s why we have created the emp.name variable and through it we are entering the value.
With the help of a dot operator, we can use any structure’s data type variable and we can store the values in it.
So, what is happening with emp.name? Employee’s name is getting stored in it.
If we go ahead, we will see that we have emp.add.hno, the variable with this name we have written.
So, what is this exactly? As soon as we make a structure, we have also made its variable.
Now through that variable, if we have to access any structure’s variable.
So, we make use of dot operators.
So, if I say that we have made, emp variable.
Not just me but for any member to access it emp, in front of it, I will put a dot and I will write the member’s name.
Here the member who is there was our a d d, add, who itself was the variable of some of the structure.
So, that’s why when we will write emp dot, after that we can put one more dot operator, which will basically go into some other structure, which means inside the address structure.
And the member over there, who can see that not the city but hno, we are accessing house number.
So, here it will be emp.add.hno.
So, what have we done? We have accessed the employee structure.
What did we access? H N O which was one member of the address structure.
So, with the flow of emp.add.hno we accessed our address structures’ members.
And we know that when we are considering the integer values over here.
So, in scanf we have to put on an ampersand.
Because we get to know then on which address, we have to accept that integer.
So, this was about the ampersand and to store h n o.
If we come ahead of it.
So, we are again considering one string and where will the string go this time.
That will go in the city member.
How will it go? With emp.add.city.
As soon as we said emp here, emp has come here, so when we did emp.add, it went to this member.
And what is added basically.
It is the variable of the above structure.
So, we have put add.city.
As soon as we have put add.city, it went here.
And the city that is the string, we accepted the string in it.
Along with it, we had to accept the pin code as well.
So, in the same flow as an integer, we also accepted the pin code.
So, this was about how to handle the nested structure, how to handle its members.
And if I have to access the other structure’s member through the other structure, how can that be done, how can we use the dot operator, all these things we have seen in this video.
Now comes the time, we have taken data input over here.
Now we also have to print it.
So, to print is also a very simple way, we have to write a printf statement and after the printf statement, we have put a colon before the name and after that the format specifier.
So whatever data was there related to the employee, in front of it we have put the format specifier and after that we have accessed the values one by one, with emp.name, employees name will be accessed, with emp.add.hno, employee’s house number will be accessed.
In the same way with emp.add.city, the employee’s city will get accessed and emp.add.pin employee’s PIN code will get accessed.
So, this was the entire complete program where with the help of nested structure we took the input of the data and then we printed it over there.
Now comes the time to execute it.
So, now we will quickly execute it, we will come to the terminal and we will clear it.
Now we will run the gcc command over here and we will write the file’s name and press enter now.
You can see that our program has been compiled.
Now we can press any character over here and we can press enter.
Now it is asking me for employee related information.
So here let's suppose we will first of all put the name learnvern.
This is a dummy name.
So here we are making the use of a dummy name, there are no employees with the name learnvern, we are using the name just for the practice purpose.
So here we have put the name.
Now we have to put the house number of the employee.
So, let's suppose we have put eight.
Now the third thing which we have to input is the city.
So, we can put any city here, we will put Delhi, suppose now we have put Delhi over here.
Now we will enter again and at the end we have to write the PIN code.
Now we will write the PIN code somewhat in this way.
Even this pin code is a dummy PIN code.
Now we have filled here dummy information and as soon as we press ENTER over here you can see that with the help of structure, we have taken all the input and after that we have also printed it, so in which way it has got printed.
The name is written as learnvern, even here it has written learnvern.
In the same way house number is eight, city is Delhi and the Pin code that we have given here was 110011.
So, in this way, we have accessed and accepted our data and taken the input and we have processed it and given the output again.
So, here are our nested structures that come in use and very easily convert the problems and scenarios in our programs and give us their solutions.
So, this was about our nested structures.
Now we will go to our presentation and we will consider one more small topic over here.
This topic is called “array of structures.”
What is this array of structures? Instead of creating many structure variables we can create an array of structures.
So, like we all know, when we make one structure, according to the structure, we have to make one variable.
We have to make a variable of the structure’s type.
So that we can access the structure’s members value.
But if I say the same program of the employee, if I want to store 10 employee related data.
Suppose I make 10 variables for each employee.
And according to that I even store the data.
But if I have to do 100 employees or 500 employees, So, in that situation for each employee I will not be able to make one variable and that would be a hectic task for me.
If I don't have to face this scenario, if I want to counter it, so that I don't have to make a lot of variables.
So, we all know that when any such situation comes where we don't have to make a lot of variables.
At that time, we make an array and the array has only one name.
With its index value, we can access the values.
So even here we will be making one array for our structure variables.
So how that can be made that we will quickly be seeing here practically, so we will go back again to our program.
Now, we will be making different structure variables, but this time we will be making it in the form of an array.
So, here the entire program will remain the same.
But there are a few things we will have to change here.
Suppose, I have to store the data for two employees over here.
So, these variables that I was making, the structure employee type.
Instead of that I will make an array here.
So, this is what we call an array of structures.
Here I'll give the array’s sizes as two because I have to store the data of two employees.
So, this was my array.
So, you can see that now I have made the array over here, but how can their values be accessed? So, to access the values, we will make use of for loops over here.
So, what we have done here is that we have put one for loop which starts with i and the i’s value should be less than two? Since, we are starting the i’s value from zero over here, so obviously i should run for less than two, only then the loop can run twice.
So, when the i’s value becomes two, even after that our for loop should go on.
So, in that situation also for loop will run thrice.
So that's why we will have to write i less than two conditions over here.
Now you will think, why are we putting the for loop over here.
So, we are putting the for loop so that we don't have to write printf or scanf statements constantly.
Since we are handling the things for two employees.
So, if the same thing was for 10 employees, you don't have to write 10 times scanf and printf statements.
So, to counter this thing basically, we have made use of a for loop over here.
So, now in for loops, how will we access the values over here? We will be accessing it basically in this way as we have made the array over here, so we will start putting square brackets to each one of it and inside those the values which are there we will write it as i.
What will happen with i.
As and when i’s value will increase, we will keep getting data for that.
So, here we can do one more thing over here.
We have to put one more loop over here as we have to even print the data.
I will again initialise from zero to i and then I will write i less than two and again I will write i++.
In this way my for loop has been made again.
We'll be using the curly brackets over here and we will write printf and scanf statements right over here.
So, you can see that over here we have made use of two for loops.
First for loop is to basically enter the employee’s data to take an input and second for loop is for employee’s data out.
So, this was our program.
We just have to convert it into an array, emp variable.
That's why we are putting i everywhere, so that as and when i’s value will increase, our employee’s data will change accordingly.
So here we will do one work, whose data we are putting here, of the employee 1 or 2? For that we will put a format specifier and we will write i’s value over here, with which we will get to know for which employee we are putting the data.
So, I will initialise the printf statement with i over here.
So, my program has been completed.
We will be taking the data for two employees and for that we have not made two different variables but we have made two size arrays, So, that’s why this array of structure is so amazing.
It helps us to handle data of different employees.
We will quickly clear it.
And again, we will run the program again, we will write gcc and write the file's name.
As soon as it gets compiled, we will write one character.
Enter employee zero information.
Here zero has come since we have started the i’s value from zero, you can write it i+1over here but for now we will keep it as it is.
Here we will write the name of one employee.
And the second information that we are storing will be his house number.
We have written it as one.
After that city.
City will be Hyderabad.
After the city we have written any pin code over here.
As soon as we press enter, it will ask for other employee’s information.
So, again we will make the other employee as learnvern.
So, this second employee is made.
Our house no.
will become two.
And the city will be Delhi again.
And here we will write any other pin code.
So, in this way we have typed the information of two employees.
As soon as we press enter, I will make this a little bigger, the terminal.
So, you can see that both employee’s information has been printed in front of us.
The 1st employee, Atharva, his information has been painted over here and another employee, learnvern's information has been printed over here.
So, this was about how we can make our structures nested along with that in place of structure variables we can make structure’s array.
What will happen with it? How many variables we have to make of structure, we can make an array's size into a big array.
And we will cut the process of constantly making the variables.
With which our program will be more efficient.
So, this was about nested arrays of structures.
The next topic will be about unions.
What exactly are unions? How are they different from structures and why must we be using them? We will be discussing all these things in the next video.
Till that time, you keep practising.
We will meet you in the next video.
Till that time, Thank you so much.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
Share a personalized message with your friends.