Hello guys and welcome.
In this tutorial you can learn about structures so structures allow you to combine variables into a
single entity.
They're very very useful.
So follow my lead and I'll explain as I go along.
So structures start with a struct keyword and we'll going to call this my struct and make sure there's
a semicolon at the end.
Here guys.
So struct my struct.
So this means we are creating a structure called My struct.
Now this is not a variable name.
This is not a structure variable we actually defining a structure right now.
So in here less to integer a choppy be just as a quick example.
So what we've done here is we've created a structure that holds an integer named a and a char character
you know 1 by named B.
And what we can do then in our main function you can do struct VI struct s.
So what is this then.
Well we've created a structure variable that is called s a structure variable called S that is basically
an instance of my struct.
So then you can do things like this s start a equals 50 s top B equals C and look at that.
So that's what a structure is.
So then you can pass this structure around to functions on stuff like that to make certain things happen.
So for example if we do void prints struct we can say struct my struct and then we can do a pointer
here guys asterisk for pointer because this is us.
This is it we're making a we are making a structure point of right now and then we can do things like
this.
So what you can do is you can go print f I space C percent I percent C and then we can go S A S B so
obviously while this is going to do is when you pass a structure to this print struct function it is
going to output variable a variable B.
Now you probably may have noticed that here we are doing a dash in a right time a while as here we're
doing a dot.
So also a lot about well in C you axis structure pointers using a using a dash and a writer but you
axis structure variables that are that are not pointers using a dot.
So that's that's that's the difference that so we can do this then we can go print struct s but you
need to do an ampersand letters complaining print Ampersand because we're getting the address of s.
So what happens here is we create a structure that is of my struct and it's called S we set variable
a.
In that structure to 50 we set rebel B to C we call print struct and we use the ampersand to pass the
address of s.
So again this becomes a number that represents the memory address of the S variable.
So what happens then is we go in here we put what we say that we want to print a integer and a char
using these %s and then we pass in the Ave ave the a value and then the p value and we use the dash
and the right arrow to access the structure pointer whereas if it wasn't a pointer if it was just like
that you would use a dot to access that.
So let's now compile that and do main.
And you can see 50 C so that is structures for you and that is structure pointers so that that is a
pretty much work similar to the way pointers would work for integers.
Apart from we don't access them using a asterisk and then the variable name instead we access them using
the variable name and a and a dash and a write out.
And that's how we can access the individual elements.
So structures can store any variable type and they are great for collecting data together.
For example let's say you are making a program that handles books.
For example you might store the book title the book description when the book was published said you
could have a struck code book and you could store all of that in there.
So allows you to organize your variables much better and it leads to a much cleaner code base and more
flexibility so that is structures guys I hope that helps.