Hello, and welcome. We are in a new section.
We're going to be talking about structures.
This is a new concept in c that allows you to group items together,
similar to array but more powerful.
I want to talk about what a structure is, how you can create and
how you can use a structure.
So structures in c provide another tool for grouping elements together.
Again, I mentioned they're similar to an array, but they're actually a bit more powerful.
If you're familiar with object-oriented programming,
if you program in java or c-plus plus or
any object-oriented language, it's kind of thought of
as an object but only with data members, no actual behavior, no methods.
So it's a powerful concept
that you will use in many c programs that you develop
because you can group different items together in a single
name and those can represent essentially data attributes.
So for example say you want to store a date inside of a program.
A date usually consists of three variables: month, day and year.
You would create those three variables to store that data.
For example you'd say int month equals 9 day equals 25 year
equals 2015.
Suppose your program also needs to store the date of purchase of a particular item.
Well, you'd probably have to go and create three more variables
for each date that you use in your program. So you can see how this can get very
repetitive and you end up creating a lot of variables.
But what you should notice is these variables are logically related. They're all dates.
They really should be grouped together.
They should almost be their own data type and then you should be able to create them
and access the different elements.
You shouldn't have to create three separate variables for every single date.
So again this is where it comes into relation to objects
and a class that has three data attributes.
So it would be much better if you could somehow group these sets of three variables together.
And this is precisely what the structure element does and c.
It allows you to group these different elements together with a particular name.
You can then create different variables of that name
and access the elements in that name,
the different data attributes, they're really called members.
So how do you create a structure in c.
It's very very simple. Structure declaration describes how a structure is put together.
What elements are inside the structure.
And we usually refer to these elements as members.
Because you first have to define the structure or declare the structure,
describe what is inside the structure.
And so you can do that with the struct keyword.
The struct keyword enables you to define a collection of variables
of various types called a structure that you can treat as a single unit.
Very, very powerful.
So by using this struct keyword, all you have to do is provide a name,
and this is usually referred to as the tag name. In this example, we have a tag name of date.
But you see the keyword struct, that's the keyword that tells the c compiler that
this is going to be a specific function.
This can have specific functionality that allows you to group elements.
So struct is the key word followed by the tag name
and then you have the left curly brace
and the right curly brace followed by a semicolon to end.
And then inside there, you can define any types of variables you want, you can even put pointers in there.
And those are called the members of the struct.
So this is statement again it defines a date structure
that the c compiler will understand.
It tells the c compiler what the state structure is going to contain,
three elements:
month, day and year. There is no memory allocation for this declaration.
Again, what you're saying is this is a structure that exists.
I haven't started using it yet. I just said this is how it's going to be defined.
This is going to be the date structure and this is what it consists of.
The variable names within the date structure, the month the day and the year,
those are called members or fields.
Members of the structure appear between the braces
that follow the struct tag name date, so tag name date.
Very, very simple syntax to group these elements together inside of a single name which is date.
And you notice it's different from an array because there's no
indices, you're not actually specifying a size.
It's not the exact same data type.
You can have different data types and you can have many different ones.
So how would you utilize this structure.
Well, the definition of date defines a new type in the language.
You can really think of it as another type just like an int, a double or float.
Except this type you defined, you can almost think of it as an enum.
Except an enum only holds one value.
This date structure can hold three values but it is another data type.
Variables can now be declared to be of type struct to date.
So that's exactly what you would do.
You could create a new variable called today
and it's going to be type of type struct date.
And today is essentially going to be able to hold three values.
And then if you need to declare more dates,
you can create more variables of that same type.
And so you no longer need to have three separate variables for a purchase date,
three separate variables for the current date,
three separate variables for a billing date.
You can have one variable
or two variables of different instances of that actual struct.
Again, hopefully you notice how powerful this can be.
So this above statement be of type struct date,
purchase date.
Memory is now allocated because you declared the variable
Memory is allocated for this structure.
Memory is allocated for the three integer values or the three members
of each variable,
so that memory is going to be allocated.
And also the reason it's going to be allocated is because there's no pointers in there.
If there were pointers in that struct, you would have to allocate memory for that pointer.
But since this today's date struct it's very simple struct I'm starting off slow,
it contains three ints, those those members are automatically allocated for you.
But make sure you understand this distinction from the previous slide and this current slide.
There's a difference between defining a structure and declaring the variables of that
particular structure type.
The finance structure again is just telling the c compiler
here's a structure and this - this is the data that's inside of it.
But actually declaring a variable of that particular
structure type is saying I'm going to start using that structure.
I now have memory allocated for the three elements in our example,
and I can access those elements and set them.
So how do you access the members in a struct?
So now that you know how to define a structure
and now you know how to declare structure variables,
you now need to be able to understand how to refer to those members of struct.
You have to access them. If you couldn't access them to see the values or set values, it would be useless.
So a structure variable name is not a pointer.
So you need a special syntax to access to members.
Because pointers usually contain just the value
of an address.
And if it's not a pointer, then you're going to need some special syntax.
You can refer to a member of a structure by writing the variable name followed by a period.
It's usually called like the dot operator.
And then after that dot operator period,
you follow the name of the member variable inside of that struct.
The period between the structure variable name
and the member name is called the member selection operator,
also referred to as the dot operator.
There are no spaces permitted between the variable name, the period and the member name.
To set the value of the day in the variable today to 25,
I would just do something like this. The name of the variable which is today,
and then I would say dot, no spaces in between either side.
And then I would specify the name of the element inside the structure definition,
which is day.
And then I would assign it just like I assign any other integer.
I would use the equals assignment operator,
a literal constant here and the semicolon.
Very, very simple. If I want to assign the year,
I would say today.year and I would say equal 2015 for example.
And when I want to access the member values.
For example, if I wanted to access the member value of month to see if it's equal to 12,
I would still use that dot syntax, that member selection operator.
So you can use the member selection operator to access the members values.
And when you access those member variables,
you can use them just like you use other variables.
You can use them in arithmetic operations if they're ints.
You would follow the rules of that data type.
So in this case, we can actually compare that into 12
just using the equals equals inside of an if statement.
And then, for example, if it was equal to 12, we could say next month equals 1.
That may be a local variable that we would assign.
So let's look at an example. Here we're defining a struct called date.
We have three elements, integers: month, day and year.
We know that we're defining it because we have the struct keyword and we have those curly braces.
And then also notice that right curly brace has a semicolon to end the definition.
At this point, no memory is allocated for this structure.
In the next line, we actually declare a variable of that type,
so we're now allocating memory for a variable name today that
contains three elements essentially: month, day and year of type int.
We can assign data to those three elements inside that
struct today by just using that dot member access.
So today.month equals nine, today.day equals 25 and today.year 2015.
If we then wanted to print it or access that data, we would access it the same way
after we assigned it. We would just use that member selection: today.month, today.day,
today.year. And again, we can perform arithmetic operations on it because it is an int.
We would use percent d or if we want to format it
to 2 we could say percent 2I or percent 2D.
So this is pretty straightforward syntax,
very easy to create and define structures and then use them
and then access them using the dot member selection operator.
How about structures and expressions?
Well, you just saw that I was able to access data and using an arithmetic expression
and it's no different.
When it comes to the evaluation of the expression,
structure members follow the same rules as ordinary variables do.
If you have members inside that struct that are integers,
you can do the same rules of any other integer.
You can apply addition, subtractions and so forth. If it's a character buffer,
you can only do the rules of character buffers. Can you add and minus character buffers,
things like that, usually not.
So floats, doubles, all the different data types,
you would just follow those same rules when you use those members inside of expressions.
So here's an example.
I say today.years divided by a hundred plus one.
Perfectly valid because year is an int,
no different than using a regular variable of type int.
You just access it differently by using that member selection operator.
Now there are variants. There are different ways to define the structures.
The one thing great about c is its flexibility
and c allows you to define a structure in many different ways. So you may see
different syntax when you define a structure.
The first example that I showed you is really the one you should follow. You should always have a tag name,
and you should define the structure in one place of your code and then
declare variables of it in another. But c being as it is,
it's very flexible. You can actually define structures and declare variables in the same line.
And you can also define anonymous structures or no named structures.
So there's a lot of flexibility. It's valid to declare a variable
to be part of a particular structure at the same time that you're actually defining the structure.
You can put the name of the variable at the bottom. I'll show you how to do that.
Include the variable name or names before the terminating semicolon of the structure definition.
And you can also assign and initialize values to those variables
in the normal fashion that you assign any variables. So let's look at that.
A variation of defining a struct doing it in the same
time as you're defining it you can declare a variable. So in this case,
we defined our struct date, but we also created a variable called today.
Because at the ending right curly brace that's the name before the semicolon,that is doing
both the creation of the variable of type date struct and the definition of the struct date.
So in this above instance,
you're creating a variable of the structure.
We refer to that also as an instance of the structure. It's called today.
It's declared at the same time that the structure is defined.
So today is a variable of type date, we did it all in one.
And you could actually right after you do the today,
you could actually initialize those variables
by using the brackets
and then saying month equals five, day equals and so forth,
just like you initialize any other integers.
Now again another way you could create a structure
is you could create an unnamed structure. This is a little bit more limited
because you can't do much with it.
But you don't have to actually provide a tag name.
Remember, the tag name is at the top, right after the struct keyword.
In the previous examples, the tag name was date.
But you don't have to provide that date.
If all the variables of a particular structure type are defined when the structure is defined,
then the name can be omitted.
And what this really means is you're only going to use the structure
one time and one time only.
So we could just say struct
int day, int year, int month
and then declare the variable in the same thing.
But you'll notice there's no tag name there after the struct keyword.
This is just one struct. We're going to use one time,
so we create the variable today and then we're never going to use it again.
And that's really the disadvantage of it and the limitation.
You really never want to do this unless you have some kind of local struct that you use once.
But you can't define further instances of this if you
define the structure this way
as a no name structure, an unnamed structure.
All the variable of this structure type that you want your program must be defined in the one statement.
You could create more than one variable you could do the today comma
and then another variable so you could create multiple variables.
You could initialize multiple variables.
But this way is a bit limited because
after that variable declaration at the bottom, you can't create any more variables.
So there's three ways to define different structures.
One way is to define the structure with a tag name
and not creating the variable. You create the variable in a separate instance when the memory be allocated.
Another way is to create the variable and the structure
and at the same time where you would have a tag name
and that would allow you to create
other instances or other variables of that struct type later in your program
or this third option where you could create an unnamed struct
with no name
and just the variable at the bottom. And again, you can initialize all of these
whenever you create either the variables just using the curly braces and so forth.
So now initializing structures.
Initializing structures is very similar to initializing arrays.
The elements are listed inside a pair of braces
with each element separated by a comma.
No new stuff to learn here really.
The initial values listed inside the curly braces must be constant expressions
You can't use variables, looks just similar to an array.
Here we're creating a today
variable of type struct date, and we're saying that
the first member is equal to seven, the second is two and the third is 2015,
which corresponds to the month day in the year
because that's the order of how they were defined in the struct.
So just like an array initialization,
fewer values might be listed than are contained in the structure.
You don't have to provide all three, you could just provide two
and that would initialize the month and the day
and it would not initialize the year.
So this would set the date.one.month to 12
date1.day to 10, but it gives no initial value to date.year.
You can also specify the member names in the initialization list this way
you don't have to actually have any order.
You can put them in any order you want and you're not dependent on that.
So you could do something like .member equals value
and struct date, date one. You could say dot month equals 12.
So the initial syntax of this is setting the member
inside the curly braces with the dot and then just assigning the value,
dot month equals 12 comma dot date equals 10.
You need that dot. That dot is important so don't forget about that dot.
You don't need the instance before the dot.
So this is just going to set the year member of the date structure
variable today to 2015
in this next example.
So here we just say because remember you can omit
some of the other member variables. And in this case, we're admitting the month of the day,
so we're just going to set the year to 2015.
In the previous case we actually omitted
the year and just set the month and the date to 12 and 10 respectively.
We can also do assignment with compound literals.
Well, what is that exactly is a compound literal.
You can assign one or more values to a structure in a single statement
using what is known as a compound literal.
I believe this might only be in C11.
But here we have today,
let's assume that we created a struct date today variable,
and then we're actually putting inside the parentheses struct date.
And inside the curly braces, we're saying 9 comma 25 comma 2015.
And this is a compound literal.
This statement can appear anywhere in the program.
So as opposed to actually initializing or assigning values using the dot member,
you can put this anywhere in the program after you've actually initialized
the today's struct. It's not a declaration statement.
This is a compound literal assignment.
The typecast operator is used to tell the compiler the type of the expression.
So you need those things in parentheses that's a cast, struct date.
The list of values follows the cast and
are to be assigned to the members of the structure in that order, it's order dependent.
So it's listed in the same way as if you're initializing a structure variable.
Again, the advantage of this is
you don't have to assign each member variable individually on separate lines.
You can do it all in one line and say okay this is the new value
for this variable today.
You can also specify values using the dot member notation as I mentioned.
You would just have to do it in one line like this
or on separate lines. And this way, it sort of looks like the initialization,
but you're still doing the compound literals.
The advantage of using this approach is that the arguments can appear in any order,
so just like previously.
Again, the key and the difference of these assignments
is it's not happening in the initialization
you're doing it somewhere else in the program to assign new values to that type.
So a lot of good information in this lecture.
We talked about structs and how powerful they are and how we can group,
different elements or different variables into a single name.
We define that structure. There are various ways to define a structure.
And then we also need to declare a variable.
Once we have that variable, we can access, assign
data to that structure using the dot member selection operator.
And then we also discuss some of the different notations
and different ways of doing things as well as initialization.
We're going to use structures quite a bit.
I'll have a couple challenges that'll help you get familiar with this syntax.
But this is just the start of the structure. We also have to talk about
nested structures, how you can use pointers and structures
and things like that.
So good luck this is going to be a fun and exciting section. Thank you.