Hello, and welcome back. Let's continue our discussion on structures
and talk about how they're related to arrays.
We talked about how structures are similar to arrays and that you can store more than one
one value, but they're different because you can have different data types
and you can group them that way.
But you can also actually have like arrays of structures.
So you've seen how useful structure is and enabling you to logically group-related
elements together, we've seen that. We know how to
define a structure with different elements using the struct keyword,
using a tag name.
And we also know how to create a variable of that type.
Because what a structure is essentially is it's a new type of data that you can use.
And then you can access elements of a struct using the member selection operator.
Well, what about if you want to create multiple structures
that is inside of an array. You can do that.
So for example, it's only necessary to keep track of one variable instead of three
for each date that is used by the program, that's the advantage of a structure.
If we wanted to handle 10 different dates in a program,
we'd only have to keep track of 10 different variables instead of 30.
Because remember, the structure contains three variables inside of itself.
10 for different variables is still kind of a lot,
so it's really not the optimal way to go. You'd probably want to create an array.
Because you'd have 10 types of the same just like you'd have 10 ints
the same. There'd just be a structure type.
So a better method for handling the 10 different dates
involves the combination of two powerful features of the c programming language,
structures and arrays.
You can use them together to make it even more powerful.
So it's perfectly valid to define an array of structures
The concept of an array of structures is a very powerful and important one in c, so pay close attention.
Declaring an array of structures is just like declaring any other kind of array.
You specify the type it just so happens it's a structure type.
So if you want to create a array of structures
that are named date, you would say struct
date and then the variable name and then you use the brackets,
just like you use the brackets for other arrays, you have to specify the size.
So in this case if we're creating 10 instances or 10 elements
of the date struct type, we would say 10 inside those brackets for the sign.
So this would define an array called my dates, which consists of 10 elements.
Each element inside the array is defined a bf type struct date,
which contains three members.
So essentially what you're doing is you're storing 30 different
sets of data, 30 different data elements.
To identify members of an array of structures,
you apply the same rule you use for individual structures.
You file the structure name with the dot operator and then with the member name.
Referencing a particular structure element inside the array is quite natural.
To set the second date inside the my dates array to August 8, 1986,
all you have to do is you specify the name of the array, my dates,
and you specify the index. We're specifying here the second date.
Remember, arrays are 0 based. So we say 1, sub-1.
And then if we want to access elements of that array
element or members of that struct for that element, we say dot month.
So this is going to give us the second element in the my dates array.
The month value for that struct type, and here we're going to assign it the value of 8.
and then if we want to access the day, we're also going to assign the day 8
because it's august 8th. And then finally, for the year, we would say
dot year equals 1986.
This is how you can assign to the second element of the my dates array,
the three different variables because it is of type struct, date struct.
You should notice that it's really not any different.
The only difference here is you have a dot. Normally, you would just say my
dates sub-1 and that would be it because you could only assign one variable
depending on the type of that array.
But in this case, you can say sub-1 with three different variables because
those are the three elements inside the struct.
Initializing an array of structures.
Initialization of arrays containing structures is similar to initialization
of multi-dimensional arrays.
Remember, with multi-dimensional arrays we would follow the specific syntax
with curly braces on the outside and then curly braces on the inside representing each row.
The curly braces on the inside were actually optional,
but it made it much more readable. And so I like to follow that convention.
In this example, we're creating a my dates of type structure date,
it's going to be of size 5. And then we're going to initialize only the first three elements.
Just like a a multi-dimensional array,
we have those curly braces inside. And so the first element in the my dates
array which is a sub-zero index. We're going to say
the month is 12, the date is 10 and the year is 1975.
And then we're going to do the same for first element. We're going to say 12 30 1980.
And for the third element we're going to say 11 15 2005.
And because we didn't supply the other two elements
in the my dates array, those are not going to be initialized anything.
So this will set the first three dates.
In the array my date to 12 10 1975,
12 30 1980 and 11 15 2005.
The inner periods of braces are optional, just like in a multi-dimensional area they're optional as well.
But again it makes it easier to read, easier to follow with those inner braces,
I would follow that convention.
So you could do this if you want to get rid of the braces,
but it's a little harder to follow because now you just have commas,
separated by commas or elements separated by commas
and it can get very confusing if you're doing a lot of those.
You have to map that correctly, say I'm in the fourth
fourth element for my fourth comma okay that's going to be
the second element of the array, it's harder to follow, so use those inside curly braces.
If you want to initialize just the third element of the array to a specified value,
you can specify my dates sub-5.
You're still creating the array here.
You're still declaring the variable and setting the size to five.
But inside the curly braces,
you're saying two inside brackets
and you're going to initialize 2 to 12 10 1975.
So you'll see the square brackets that represent the element that you want to initialize,
then the assignment operator equals and then inside the curly braces
you're initializing the the month, day and year
for the elements inside the struct.
So you can specify exactly what element you want to initialize as well.
If you want to just set the month and the day the second element of the my dates array to 12 and 30,
you can do something very similar.
You just do a comma to separate each attribute.
You don't want to actually set the year so you want to be able to use those curly braces.
You could say dot month equal 12
sub-1 dot day equals 30.
And again this syntax looks very similar to initializing
struct variables elsewhere.
The only difference here is because it's an array, you have to specify the index of the array,
but similar syntax.
What about structures containing arrays.
Elements inside the structure that contain an array.
It's also possible to find structures that contain an array as a member.
This might be useful.
It's the most common use is to set up an array of characters inside a structure.
We use char arrays quite a bit right for strings.
So it would be very common for a structure to contain a string essentially,
a character array.
So suppose you want to define a structure called month
that contains as its members, the number of days in the month
as well as a three character abbreviation for the month name.
Well, that's where we could use a character array. We could say struct month.
We know how to define a structure. Again, this is not allocating any memory.
This is how the structure is defined. These are the elements that are contained inside of it.
Here we're using the struct keyword. We say the name of the struct. The tag name is month,
and then we have that left curly brace and then we specify the elements.
We have one element which is integer represents the number of days we have a semicolon at the end.
And then we have a character array, character name sub-three,
so size of three because it has three characters.
And then we end the structure with the right curly brace in the semicolon.
But remember, characters are arrays and they're closely related to pointers as well we know that by now.
But you can easily have structures contain arrays
by just putting them as a member element.
And so this sets up a month structure that contains
an integer member called number of days and a character member named called name.
Member name is actually an array of three characters, pretty straightforward.
So how do you actually access it. Well, first you'd have to create your variable.
The variable would create memory for the elements inside the struct
and then you'd want to just set the proper fields inside the 8 month
variable, the character array because that was the name of it. We had name
and number of days. Here it's called name and the example it's called a month.
Well, a month is the variable type and then name is the element type, so that's correct.
So here we create a struct. It's of type month.
It's the variable name is a month. We don't initialize it with anything.
We immediately set a month number of days of 31.
We use that dot member selection operator.
And then we want to assign to the character array,
we can't assign directly to a character array.
You'd have to do a str compare or something if you wanted to sign everything. And you could do that.
You don't have to use these indexes. You could say str copy
a month dot name comma
and then j a n in double quotes or you could do it this where you access the elements
of the array via the index.
Here we're saying a month dot name because we have to access the name member variable
or member element.
And then inside the brackets, we specify the index of the array.
We'll find it signing the first character in the array of j,second one a, third one n.
You would see this syntax if you were doing ints
and doubles and floats. Normally, when you have a character array,
you'll use the str stir copy function and assign everything with double quotes.
You can also initialize the variable to the same values
by doing something very similar when you declare the variable. Just inside the curly braces,
because it's an array, you can have inside
inner curly braces and then the single quotes around each character.
And you can set up 12 month structures inside an array to represent each month of the year.
So now we can create a month's variable,
which is an array of size 12 of type struct month,
and that can represent each month.
And then for each month, we can spend the number of days and then abbreviations for the month.
So utilizing these two powerful concepts
of arrays and structures is very, very useful.
Structures can
have many elements, so they can actually be a structure of arrays
and elements inside the structure can be an array.
And we're going to next talk about how we can use structures to contain other elements like pointers.
But you'll see that structures can store any type of data
in many different types of data.
And it's very, very useful to use with the array concept.
I hope this was helpful. Again, we'll have a challenge to demonstrate
some of these concepts and bring up some examples
inside code blocks later. Thank you.