- In this video, we will further our understanding
of how structure work by declaring them and accessing them.
Let's look at an example of a structure that could be used
to designate the characteristics of a person.
We will call this struct 'person',
and we will assign five fields.
Two of them being first name, last name;
followed by age, eye color and height.
Notice that we have three character arrays, one integer,
and one float, yet, all this five fields
are packed into one type.
For all intense and purposes,
they are treated in the same manner
as we would treat a many regular type
that we have seen so far that are built into the language.
This is how we would refer to a struct.
First, we are going to declare a variable of the same type
as the struct.
So, 'struct person citizen' means that
'citizen' is now a variable of type struct person.
We can now refer to the fields of 'citizen'
using dot (.) notation.
'Citizen.age' refers to the age field
or the age characteristic of the struct,
and is being initialized to 38.
Those are for all intense and purposes,
regular variables except they are part
of a more complex variable called 'citizen'.
Let's see how this works in code.
First, we are going to declare two constants
that will help us with arrays,
which are going to be part of this struct.
And, since we're going to use string manipulation,
we're also including the extent of library 'string.h'.
This is the array, 'firstName'. The array, 'lastName'.
The array, 'eyeColor'.
Those are the three arrays that they're using
that define constants in line 16 and 17.
First name, last name, age, eye color, and height,
together are packed into one type person.
We are also defining a function print structure
to allow us to print this complex variable.
First, let's initialize the struct
with some data using the variable 'citizen'.
So, 'citizen' is now a variable of type struct person,
and we can populate its fields:
age, height, last name, first name, eye color.
Notice that, while 35 and 36 are regular variables
that can be populated through a simple assignment statement.
Last name, first name, and eye color are strings.
To populate the string, we are using the 'str' copy command,
which we already saw in the video dedicated to strings.
We are assigning Johnson, Sasha, and Green respectively
to the three corresponding arrays of characters or string,
and we are now ready to call a print structure function,
which I define specifically
to be able to print this structure.
Notice that I am passing the 'citizen' argument
to the print structure function
as if it was a simple variable.
The function is defined as having one parameter,
which is named individual and it's of type struct person.
Inside this function, we have five 'printf' statements.
Each one corresponding to the type it's printing.
For example, first name and last names are strings.
Therefore, we are using the directive '%s' in 'printf'.
Age is an integer, and we're using the directive 'd'.
Height is a float,
and we wanted to only have one decimal point in line 51,
and eye color, again, is a string or an array of characters.
Let's run this program and see how this looks.
As you can see,
the values with which I populated the array are now printed.
So, if you want to print a single value
or several discrete values, you use the regular 'printf'.
Now, this program gave you an example
of how to define a custom type,
initialize a variable of that custom type,
and then create a printing function
for that particular type.