- [Voiceover] We are going to advance our knowledge
of structures, and we'll see how we can manipulate them,
treating them as the complex variables that they are.
In this example, I am defining an array,
"teams" of size 10, in which each
element is a struct person.
So, the type of the array is "struct person".
In order to access or assign a particular
value to one of the elements of the array,
I have to use the regular array notation, in this case,
"teams[0]", the first element of the array,
dot, the field of the struct
that I wish to change.
So, teams[0].firstName = "Bob"
This means, the first name of the first person
in the array is Bob.
teams[5].age = 30
The age of the sixth element of the array,
which is in the position five is now 30.
I can also use the for loop in order,
for example, to print the array.
I can run a loop with an index from zero to nine,
because i is less than ten,
and there are 10 elements in the array from zero to nine.
And then print all the last names
and the ages of the elements of this array.
In a way, I can treat this array as a mini-database,
and we can see now how a lot of these
concepts are coming together.
Arrays and variables and loops
and structs and types and so on.
Let's see how we do that in C,
and we can do even more interesting things with it.
In this is example, we will see, how can we
access arrays of struct
and treat them as if they were a mini-database.
First I am defining a struct named "person",
similar to the one we used in the previous examples,
line 24 through 30.
Then, I am creating a bank of names.
Those are three two-dimensional arrays
of characters because they are arrays of string.
Please recall that a string is itself
an array of characters, so in a sense, what we have here
is an array of arrays,
hence, in line 33 through 35,
we are using the two-dimensional array notation.
So, the bank of "firstName" has one dimension,
MAX_NAMES, which is defined above here.
as 14, and a second dimension
of MAX_NAME_LENGTH.
This is the maximum allowed string length
in that array.
The same goes for the bank of last names,
and the bank of eye colors.
Now, I'm going to use two functions.
One is to print a structure.
We have seen that before.
And one is to populate array of structs.
Let's look at a populate array of structs.
Essentially what I'm doing here, I am choosing
random entries from the bank of data
and populating an array of structs,
which I define as a global variable.
That array is called "teams".
First in line 53, I'm seeding
the random generator with a current time.
You may have seen that function before,
if you looked at the challenge from the previous chapter.
Then, I am starting a loop that starts at zero
and runs through the entire length of the array "teams",
using MAX_TEAMS, predefined as the
upper boundary of that array.
Now I have to choose a random number
in line 57 that has a value within
the range of the bank of "firstNames",
line 58, a random number within
the range of MAX_NAMES.
Then, in line 59, a random number
for a position within the range of the array "colors".
Therefore, I can now use those random numbers
as index positions in their respective
bank of data.
And then assign that to the team
in the i position, because I'm now in the for loop.
So at every cycle of the loop, I am selecting
random positions from my bank of names,
eye colors and so on, and I'm assigning them
to the team's array.
So, for example, line 61, I am choosing
a random number from bank of last names,
using rL, which is the randomly
computed index for that array.
And I'm assigning it to
teams[i].lastName
Finally lines 63 and 64, it's a straight-forward assignment.
teams[i].age, teams[i].height
I'm choosing a random number for age,
because I am defining above MAX_AGE to be 120,
So, any age up to 119 would be okay in this program.
And then, line 64, it's a little bit of a calculation
because I'm trying to pick a random height.
Because I want to display the height as inches and feet,
I'm going to pick a random number for inches
and a random number for feet.
The random number for feet is four
plus a random number less than three,
so zero, one, and two, which means the feet could be
either four plus zero, four plus one, and four plus two,
so I'm allowing to a maximum of six feet,
plus a random number between
zero and nine in inches.
Finally, the print structure is the same
as in the previous example.
Let's print this and see what it looks like.
Notice the array is nicely formatted, because I allocated
spaces according to the size of each data type.
For example, in line 71, I allocated
eight spaces for first name, line 72,
nine spaces for last name.