Hello and welcome back.
More on structures, particularly nested structures, nested structure structures inside structures.
That's what that means.
So see, allows you to define a structure that itself contains other structures as one or more of its
members.
It's a pretty neat concept here.
You can have structures inside of structures.
So you've seen how it's possible to logically group the month a day in the year into a structure called
date.
We've been sticking with this example of date.
Well, what about if we want to actually group time, the hours, minutes and seconds into a structure?
That seems like that would be useful as well.
So here we would have a structure called time and it's very similar today.
It contains three elements.
We know the name of the structure is time because it follows the struct keyword, that's the tag name.
And then inside the curly braces, we define our elements, our data types and the names of our variables.
And we know now that we can also put an array in there if we wanted.
But in this case we don't need an array, we just need three integers.
We end the structure with the right curly brace and the semicolon.
What about, though?
In some applications you may want to actually group both the date in the time together.
It would make sense, right?
Because the date and the time are logically organized or logically together, right?
They're logically grouped.
So you could set up a list of events that are to occur at a particular date in time.
And so this might be a very useful case where you'd want to use a nested structure.
So you have a convenient way to associate both the date and the time together.
We can define a new structure called, for example, date and time.
This would contain, as its members, two elements a date and time, which are both structures.
So we now have a nested structure.
We have a structure named date and time that contains two other structures.
Something like that in the syntax is very similar to creating other structures.
The only thing difference here is inside the actual structure as the element names.
You have the struct keyword, the name of the struct, and then the variable name.
So in this case, we're creating a date and time struct and we have two structs, one ones the type
date, ones of type time.
Two variables state and S time.
The first member of the structure is of type struck date and it's called SE Date.
The second member of the date and time structure is of type struck time and it's called se time.
Variables can now be defined to be of type struct date and time.
We don't have to create separate variables for date, separate variables for time.
We can create one and that structure is going to store both the date in the time.
We would do that just like this.
We would use a strong keyword, specify the type of the structure, date and time, and then the variable
name.
In this case, they're going to store events.
So we say event when we want to access the memories in a nested structure.
Again, pretty straightforward syntax.
All you have to do is reference it like any other member.
You have to use that member selection operator.
So would use it pretty much a number of times.
So if we have a structure named event and we want to get the date, we say events date because that's
a member of that date and time struct.
Now, if you want to reference a particular member inside of a state again, you you would use the period
file by the number name that would be tacked on the end.
So this below statement is going to set the month of the date structure contained within event to October,
and it's going to add one to the seconds contained within the time structure.
So we say event state and then another dot because we want to access the element of the state variable.
We want to access month and we're going to assign month ten.
So there's multiple dots there.
And all this is saying is you have a struct named event.
And because of our declaration before, we said it was of type, date and time.
Inside there you have a struct, which is just a date struct you want to access of that date struct
the month member.
So you say dot month to Dot's.
And then if we want to change the second increment by one, we can just say plus, plus we can use that
pre implementation operator plus plus event as timepoint seconds.
We have to include those two dots and seconds is an integer, so it's just going to add one to it.
Very straightforward.
The event variable can be initialized just like normal structures.
If you want to set the date in the variable event to February four, 2015 and the time to 330, you
would say start date and time event.
You would create it just like normal.
And then you'd have those curly braces to specify the different structs initialization members.
So on the first curly braces, we're going to initialize the date, we're going to initialize the month
to two February, the data one and the year to 2015.
And then we end the curly braces and we put a comma after those curly braces.
And then the second, which is kind of look in it looks like a multidimensional array initialization.
We say the time is going to be equal to 330 with zero.
So 3 hours, 30 minutes, 0 seconds.
You can use the members names and initialization just like all of our other initialization rules.
This would be beneficial because you won't have to specify the order.
And here we just create the event.
And then inside the curly braces, we use the dot.
Operator.
We say month equals two days equals one year equals 2015.
And that is only going to specify the date initialization.
If we wanted to specify the time initialization, we just again use the dots for each member name dot
dot minutes and we could switch the order around.
Right?
We can say dot second, SQL 0/1 and then minutes equals 30 and then our three B last.
The order does not matter.
And then we end it with the semicolon and the curly.
Right, curly brace.
Now that we know we can have nested structures, what about an array of nested structures?
Now we're really getting into it.
Yeah, it's possible.
You can set up an array of date, time date and time structures and date and time contains two structures
inside of it.
You just do it just like another normal syntax.
You have to use those brackets to specify the size of the array.
When you declare the actual structure name, you have to specify the of type date time, just like any
other structure.
The array events is declared to contain 100 elements of type, struct, date and time.
The fourth date and time contained within the array is referenced using the usual index sub three or
index three or subscript three inside those square brackets.
So to set the first time in the array to noon, you would say event subzero dot SW time because remember
SW time is pointing to the time struct and then you have to access the element in the time struct,
which is hours.
So you use another dot and you say 12.
It looks very similar to the previous declaration or assignment I should say, but instead it has that
array index zero inside the square brackets.
If events was not an array and you just wanted to access the single structure or the hour structure
of time, you would just get rid of the brackets.
So the arrays do not really complicate anything.
You just have to be really familiar with those square brackets and the indexes.
Here.
We're setting the minutes to zero event sub zero.
So we're setting that first element and then we're setting the seconds to zero.
Now if we want to declare a structure within a structure.
You can define the date structure within the time structure definition.
So this is another another category of flexibility.
We don't have to necessarily have the date and the time structure defined somewhere else.
We can define those structures right in our date and time structure.
So here would be an example of just defining the date structure inside their inside the time structure.
So here we have a nested structure, but we actually defined it inside the definition of time struct.
So we say struct time with the tag name and then inside the curly braces, we say struct date.
That's perfectly valid.
And you'll also notice that the end of the start date before the ending semicolon, we're creating a
variable called DOB.
And then after we create the start date and its elements in a variable DOB, we specify the elements
of the time start hours, minutes and seconds.
So that is perfectly valid.
Now, you should probably notice the limitation of this struck date is only going to be defined in there
once with a variable name DOB.
You will not be able to use the date structure anywhere outside of this.
Unless you use an inside time you will not be able to use date by itself.
So the declaration of the date struct is enclosed within the scope of the time structure.
Definition scope refers to how you can access things because the date structure is in the scope of time.
You cannot access it unless you create a time variable time struct variable.
You can't just say struct date my variable name because you don't have access to that.
So date does not exist outside of the time struct.
It's impossible to declare a date variable external to the time structure.
The only time you can ever use a date structure variable is when you create the time struct and it will
automatically create a DLB variable for you.
So this is somewhat limited, but you would use this in a case where you only need the date when it's
associated with the time the date should never be by itself.
Then you would do it this way.
So another knee concept, a concept of providing a structure inside of a structure very, very basic.
And the syntax is pretty simple.
You'll get used to these pretty easily, adding the array to a structure that contains a structure.
You just use the square brackets in the index.
Nothing really changes there.
And then you can change the format a bit.
If you only want a local nested structure, you can define it right inside the other structure definition.
So we're really becoming experts here with our structure type.
I want to talk about a last couple concepts, particularly I want to show you how you can create pointers
inside of a structure which is useful.
And then we'll do some challenges and exercises and it will really get you really familiar with this
structure concept.
Thank you.