Welcome back guys.
So now we're going to learn about a raise.
So what is an array an array allows you to define a variable with a given name but have it so it can
store multiple of the same type.
So it's best I show you this in code so you can see it clearly.
Let's say I wanted to take a name at some point in time from my use.
Well what I would do is I would create a character array for the name and in here goes the length.
So Shawn name 20 with these brackets as well.
So what that means is a name variable that has a type of char.
So it can only store one bite at a time.
And we wanted to have 20 bytes.
So in other words we can store 20 bytes in this named variable.
So how does that work.
Then if char and only stole one byte Well what happens is the compiler looks at the data type it sees
as a chore.
So in those is one byte then it sees this number and it knows that we want to have 20 of them.
And it also knows we want it to be able to be referenced by the same name since char name.
So it allows us to access 20 bytes using the same name.
So how do you access them then.
Well like this Let's say we wanted to set a name to a like we did in previous tutorials.
You can see a Sarah.
Why.
Because this is an array we can't we can't set it like this.
So if we did name 0 equals A.
That works fine.
So what's happened here then is we set the first bytes in our name array to zero because in C indexes
start from zero and you access indexes and arrays so in this case what we're doing we're accessing index
0 from the name array and we're setting it to a C you know you could do the same here one equals B name
two equals C and now if you wanted to output that you could go print f s I mean c side C for Kent for
char and then we go here and we go name 0.
So while I don't do that I put a.
If we put a one here it would output B we put a two here.
Who would output C.
Now there's rules for these indexes.
Firstly they can't be negative.
Secondly index is always stopped from zero.
In other words if I wanted to access byte 1 of this name array I would use zero.
The third rule is always the fourth.
You cannot exceed this length.
So I want to access the last bytes of this array.
Would I do name 20.
Absolutely not.
I would do name 19 because they start from zero.
When we access it we always start from zero.
So name 19 equals D would set the last byte of this array to D.
Now in most languages like Java and stuff like that.
If you exceed the buffer the buffer is another word for array.
By the way if you exceed it what'll happen is it'll throw some sort of exception annual program will
gracefully end in c.
It doesn't work like that and see if you exceed this buffer at best case scenario your program will
crash.
Worst case scenario a hacker could use this to their advantage and it really is that serious because
in C when one is compiled there's a bunch of return addresses and stuff like that stored on the stack.
And right now we saw this array on the stack and how the stack works is when you enter a function it
allocates some room on the stack for all the bytes that are in the scope and the scope is anything between
these brackets.
That's what a scope is.
So in our case it will allocate 20 bytes on the stack for us and it will also allocate some bytes for
these variables here for our function prototype.
Now the stack memory is allocated by simply Dec discriminating the base point to all stack pointer.
And the problem with that is is you can also run out of space very easily because we're not asking the
operating system for to give us some some data here.
We're not doing that.
What we're doing is we already have the data and we're just literally just moving the pointer down 24
28 bytes.
And that's what we're doing.
And then that gives us room to set these things.
So what happens is when you leave the function it resets the pointer back to where it was and now once
it once it does that everything here gets naturally freed.
And when I say freed I don't mean deleted.
It's just kind of like it kind of it ends up in a kind of like undefined state and it can be overwritten
by other function calls and stuff like that.
But you don't need to worry too much about that.
That's right.
Quite a complicated subject.
But the point is I'm trying to make is you have to be careful how much you allocate on the stack because
the stack can only take so much.
And secondly back to our potential problem where if you go over this buffer an experienced hacker could
offset this just right to override the return address.
That will be on the stack.
And what the return address does is it basically holds the address to the last function that calls you.
So then when this function finishes it will pull that address and go back to where it was executing.
And the problem with that is a hacker experience hacker could overflow this buffer is called a buffer
overflow you open to look it up.
And when they overflow it they could overwrite that address to point to somewhere else.
And that's why it's very dangerous.
So in C you always have to make sure you check that anything you're accessing here can fit.
So for example let's say I had an Integer i equals 50 and I did name i equals E.
So what will happen is that it would set it would go into I would pull 50 and it would set index 50.
But the problem here is we can only store 20 bytes.
So what happens is it'll crash the program but experience hacker could change that and do nasty things.
So how you get around that is you can basically check that I can fit into here.
And if it can't then you don't set it by any way more to conditions later on.
Just wanted to get that out there.
So what what what we can do now then is we can test this.
So give it not name 19 if you did that because that'll crash the program right.
And we're going to open up command prompt CMT and I'm going to copy paste this address by going C D
and we're going to right click enter and now you're going to go GCSE main dot c dash of Main and I should
compile find two main now and you can see the output C because we're out putting element two in the
array which is C so that's how arrays work.
It allows you to access the same variable name and multiple bytes.
So that's that's literally all it is.
So basically rather than having 20 different variables named name one name to name three names for name
five.
You can just do name and then do 20.
And it really is that simple and it's very efficient as well.
Well you can access these and any in a number of different ways like such as using loops that is without
a raise the language wouldn't really work very well.
So that's why arrays are.
And basically that is that is it for a raise.
You can have arrays of all different types.
I could have an integer array for example integer ABC 30.
So now I've declared 30 integers and I can go ABC zero equals fifty five ABC one equals twenty two.
So that is what arrays are a very very handy feature of the language and you can see why.