Hello, and welcome back.
In this lecture, I want to provide a demonstration of a solution
for our previous challenge.
So what I want to do is I want to show you how you can declare a structure,
also define a structure, its contents and then initialize it, and then
read in some data and print it out.
So this will get you familiar with the basics of structures.
Here we have code blocks.
I'm going to create a new project.
And I'm just going to name it something simple, console application.
Let's name it basic structure example.
And this will create our main force.
We know all about this.
It will create include two files as well, and we're going
to delete the hello world.
So I wanted you to create a structure of type employee or name
employee with three elements, a name, an employee id and a salary.
Actually, I think I wanted you to create a name, hire date and a salary.
So to create a structure in c, all you have to do is use a struct keyword.
And we want to define it outside of the main so that we
can use it in other functions.
It's a global definition, but it's no big deal because you can still
create the instances or the actual variables inside each function.
So we say struct and then we provide a tag name.
If you remember from the lectures the tag name is actually optional.
You don't have to provide a tag name if you're just creating a local
structure with just a single variable.
But since this is going to be used in multiple functions, we're
going to provide a tag name.
We're going to provide the tag name of employee, and then we're going to
have our left and right curly braces.
It automatically have the semicolon for us.
We could create a variable instance at the same time we're
defining the structure by just putting a variable name here,
but we're not going to do that.
So here I want to just create a name.
It's going to be a character array.
I'm just going to place it as 30 characters.
I'm also going to have a hire date.
I think I said the hire date should be an int.
But you could make it an int or a character array.
I'm going to put it as a character array, and we'll make that
just 15 as well and then our salary which will be a float.
So what we're doing here is we're creating this nice structure.
Structure is great because you can group things together in
a single variable and you can also use it with arrays and you
can store a lot of elements.
But it's nice that we now have this employee structure that
has data associated with it.
Now to actually use a structure, you have to create
a variable instance of it.
So here we're going to just create a structure employee type.
All we have to do to create a structure employee type is use
the struct keyword followed by the name, which is employee and
then an instance or variable name.
And here we're saying we have an employee Mike, and we're going to
actually have the second parameter.
We're going to define it as a date inside quote, so we're
just going to say 7/16/2015.
And then the last thing is their salary 76,909.
They're making pretty good money.
And then we want to print this out.
You'll notice that in line 14, we didn't create a pointer.
So in order to access employees struct, all we have to do is use the
dot modifier or the member access.
And then we just use the format specifiers for each
variable type which is percent s percent s and percent f.
So this is pretty straightforward.
To print out the data inside the struct that we already initialized,
we can just pronounce some line feeds and format specifiers.
Again, the way that you access the structure variable is you
just use the dot and that will give the element inside of it.
And we initialized it with the curly braces with comma specif
separate separated the variables.
So let's try building this and see what the output is.
It should be exactly what you think it would be which is
what we initialized it to.
We do have an error.
We don't have an employee id, got to make sure I used right ones.
This is a date.
I had different variations of this program decided to use a date, and
it's also going to be a percent s.
So now when we build, hopefully, we're okay, and we are.
So when we run this, we're going to get out Mike ID.
I got to change that date 7/16/15 and then our salary.
It'd probably be nice to do some formatting here.
We want to change this to hire date.
And we probably just want to say dot 2 for our format.
And now we run it the format, the data output looks a
little bit better, 76,909.00... 7/16
Everything looks good.
So now let's read in from the keyboard some of the values
and stored in the struct.
We can still use the same instance or the same variable name.
Here we're just going to tell the user to enter some information.
So we're going to say printf, and we'll just add some line feeds.
And we're going to say enter employee information.
And we'll end it with a line feed, a semicolon.
And then we're going to say printf name and end it like that.
And then we're going to get the actual name.
We're going to use gets since we used it a couple times already.
Let's use scanf.
We're going to say percent s and that is going to read in the employee name.
So all we have to do here is employee.name and this is a
character array, so we don't have to provide the address.
And then we're going to say printf.
After they entered their name, we're going to say
printf we'll add a line feed.
And then this time, we're going to say hire date.
And then this time when we do a scanf, we're going to do a percent
d or actually percent f for float.
And then this time, we need the address of operator because this
is not a pointer and the scanf function does take a pointer so we
just need to say employee.employee hire date right or date.
And then we want to print out the salary, tell them
to enter their salary.
I want to have a line feed for that, and we want to use scanf to
read in and the date is in percent s and the salary is a percent f.
And the date is wrong again, don't need the ampersand for the date
because that's a character buffer.
I need the ampersand for the salary, just like that.
We could probably add some line feed, so it's a little bit easier
to read, put some comments in there.
And then we just want to print out the information.
We can print it out just like we did before.
So we could copy and paste lines 16, 18.
Just do a control+c, control+v. And now we're good.
Let's try to build and see where we're at.
So it's first going to- when it runs, it's going to output the information
that we hard coded or initialized.
And then we're going to enter some information.
So for my name, I'm just going to say Jason.
The hire date, I'm going to put 8/9/10.
And the salary, I'm going to put 32,000.
And it prints out, Jason 8/9/10, 32,000.
So this is just a simple example of how you can create structures
first defining them globally and then creating an instance of it or a
variable type of it and then printing out the information and entering
information, keeping aware that we need to use the ampersand, the address
of operator when we input data.
And in other instances, we don't need to do that for the character array.
I'll provide a solution for this as a zip file.
Good luck, and please ask questions if you need to.
Thank you.