- [Instructor] Welcome back.
In this video, I want
to review with you how
to create new types with C.
There are three options
that I want to describe,
typedef, enum, and the struct type.
Typedef allow us to create
an alias for a type.
For instance, in the example,
booOoolean or FlagType
I used alias for int and char.
So x is a variable of type int,
and y is a variable of type char.
If you see booOoolean or
int, it's exactly the same.
If you see FlagType or
char, it's the same.
You just create a new name for
a type that already exists.
As a second example here,
cat and dog are alias
for int, and letter is an alias for char.
So later you can create this
variable x that is a letter
or char and you can create
variables one and two
and use as a type cat, but
they are really integers
so you can assign the value
one or two to those variables.
Typedef can be used can be used with enum
to create an alias for the
integer type and to define alias
for the values that are valid.
For instance, in the first
case, booOoolean is an alias
of int, but there are two
values that are valid,
false and true.
False is a label for zero,
and true is a label for one.
Also, days is another
alias for the int type,
and we define that the values to be valid
are those in the list, but
basically they are numbers
where Sun represents zero,
Mon represents one, and so on.
So in the example, you
could create a variable a
and make it equal false, but that variable
is really an integer and
it has a value of zero.
Then x and y are days.
Days is an alias for int.
And when you do x = Mon
that means x equals one,
and y = Fri, that is the
same that y equals five.
So when you do today, that is
also type days, an integer,
equal to x plus y, that
is gonna do one plus five
and the result is gonna be six.
So when you print the value of today,
you're gonna have on the
screen the value of six.
When you create an alias for types,
that tells you for the programming.
And the same when you create an alias
for the values of a type.
That tells you in the
programming to remember some kind
of concept like days and
the name of the days.
But from the programming language,
you're working with numbers,
integer variables and numeric values.
Because they are numeric
values, you can use them
in any expression or a statement.
For instance, you can use
those values in conditions,
like here, so you can compare the variable
with any of those values
like var with Yes.
Or you can use the variables
like x in switch statements.
So in this example, x is
compared with the case amber,
with the case red, and
with the case green.
And again, they are numeric
values because red is zero,
amber is one, green is
two for traffic_light.
And for logic, No is equal to
zero, and Yes is equal to one.
Struct allows us to create a type
that is a collection of variables.
This is like a class in Java,
but without methods.
We use the keyword struct,
follow it by the name
that we want to use for the struct.
For instance, if we want to
create a record for a person,
we can create a struct person.
Then inside curly brackets,
like you did with classes
in Java, you're gonna
put the list of variables
that are gonna be part of this struct.
You put the type and the
name of the variable.
For instance for the person,
if you want to store the name,
you can just write char,
and we can add an integer variable id.
And you can also see the struct
is created outside of the methods.
Then we can do the struct
to create variables.
For instance here, we're creating
two local variables, x and y.
They are of type struct person.
Notice that we use both
words, the keyword struct
and the name of the struct
that we created before.
And you could find familiar denotation
in which you put the name of
the struct, in this case x,
dot, and the name of the variable
that you have inside of the struct.
So you can use x.name and &x.id
and store values in those variables.
Also if you want to print
the values of the variables,
you can use x.name and x.id
inside of our printf instruction.
Obviously we can create
global variables from structs.
Moreover we can create
arrays of the structs.
For instance, here we can
create this array contact_book
with 100 elements, and each
of the elements is a contact.
And contact is this struct
in which each contact has a name,
a phone, and an email.
Again, name and email are arrays of chars
because that is their
presentation of a string in C.
Later in main for instance,
you can access each
of the elements in the
array using an index
inside of a square bracket, as usual.
And you use the dot to
access the variables
inside of the struct.
So you have this contact_book, the index,
that is the position of
the element in the array,
.phone, .name, and .email
which is the same notation
that you could use in Java
to access an array of objects.
Finally, something that we can do is
to create pointers to structs.
Like we did with other types,
in order to create a pointer
to a struct, you put the
asterisk between the type
and the name of the variable.
In order to access the data in the struct,
you use the asterisk also, like here,
and then the dot and
the name of the variable
inside of the struct.
However an equivalent
notation like this one,
asterisk, name of the struct, dot,
and the use of the arrow, like here.
When you have a pointer to a struct,
you can complete the asterisk and the dot
for a minus and a greater that symbol.
That together represents this arrow,
so you can use the pointer, the arrow,
and the name of the
variable in the struct.
And that's it, see you next week.
