Oh guys and welcome back.
Well now going to learn about memory addresses and pointers.
So firstly know that everything in C has a memory address.
Functions have memory addresses variables have memory addresses literally everything has memory address
once it's compiled.
So we can access those memory addresses to do really cool things.
So for example I can access the memory address of oxy here and change it anywhere in my program.
I could access the memory address have a function and be able to call that function even without knowing
the function name to begin with.
But we can get on that later.
Let's now start memory addresses and pointers.
So firstly do you print f percent p four pointer and just output in the memory address right now of
oxy.
So that's all I want you to do.
You do Ampersand oxy and then obviously junior from problems but going to see deep into our uh hello
world app again
we're gonna go JCC may not see Dash o main and then we're gonna remain I think a C outputs the this
this hexadecimal number here.
So this is the memory address.
At C is at when you load this program and Oxy is a stack variable which means that it gets deleted when
we leave this function.
And that's important to note because it means if I shared this this address around the program as soon
as I leave this scope that address is now dangerous and you shouldn't be accessing it.
So that's important to note.
Stack Stack data should only be access when one is safe to do so.
So in other words if this function main calls another function that calls another function calls another
function and so on.
This address can safely be accessed but if I stole the address Foxy somewhere globally and then I leave
this function.
The second I leave this function that address is now unsafe.
We can't access that address anymore because the stack phone to remember I was saying about the stack
pointer it gets pushed back up the base point a sack point that they get pushed back up and then that
that that data is no longer accessible but you can access it.
It is unsafe to do so.
Right.
So that briefly explains that.
So essentially if we do print f let's do less to ensure a equals 50 and then we go print f I'm just
gonna do I for both of these takes time rather than P just to demonstrate this.
So you can see we have put the address as an integer and we output the value of a says compile that
again
so you can see when we use the ampersand that gives us the address as a decimal and that's the memory
address of a.
And then we don't put the ampersand.
So outputs the value of a.
So I hope that shows that by using the and Professor Ampersand you can get the address of things.
OK.
So now point is.
Let me explain.
Point is to you.
This is very very important parts.
Now remember I said stop when we first started this course.
I mentioned I tell you these past Christmas mean wow.
That point is.
So let me explain that to them.
If you do integer asterisk P just four point two that's a variable named by the way equals M percent
A.
Now what we've done here we've created a point a variable and point to variables basically encourages
the whole memory addresses and we've created a point of variable that points to the address of a and
remember remember earlier we were testing output in the memory address and the value.
So remember in this case then you know this memory address would go into P.
Right.
And because we know the memory address we can overwrite the memory address essentially overwriting variable
a.
So it's called a pointer and obviously pointers have data types.
So in my case integer so it means that when I access this this memory address it will be treating it
as an integer.
And that's why this data type is pretty important here.
So I want you to do asterisk p equals twenty.
So what does that to them.
Well without the asterisk we just change the value of the pointer and remember the value of the pointer
is the memory address to what is pointing at in our case and percent A.
Or the address of 8.
Now when you put asterisk before the p by here you are saying no no no don't set the memory address
don't set the value of p set the value that P is pointing to that is what you're saying.
So once again without the asterisk set the value of our point a variable to address 20 with the asterisk
set the set the value that P is pointing to to 20 and our case is pointing to variable a so guess will
happen a will become 20.
Now just to demonstrate this I want you to print f I back session for new line.
Not now required but you can do it passing a f so this will output the value of a which is 50 but we
change it here to 20 with our pointer variable.
So if you go back now and compile that run again you can see 20 output it.
So you can see that we've successfully changed the value of a by using a pointer variable and now to
explain the one you have to asks asterisks instead of one what you're saying is you are creating a pointer
of pointers that is basically what you're saying.
Okay.
So now I want to show you something else.
You can access arrays as a pointer as well so follow what I'm doing.
Let's get rid of all of this.
We're gonna go to char buff for buffer 512 or whatever it can be 20 whatever how many by two ones 20
then.
And then what we're gonna do is we're going to say
char asterisk four pointer equals.
I remember you wouldn't.
Don't use the ampersand here either because it's a ready right.
So just by going it will give us the address.
So now you should be able to do things like this point zero equals a point to 1 equals B
pointer to equal C and then if you do print f percent C for char and then you go above 1 we should see
a B let's give that a go
main we see B so that is how you create pointers to arrays.
So essentially you just do that and you can access them like this.
That's the thing about the array index in C you can access them through pointers such as 0 1 2 on the
pointer and it'll pick the correct element.
But remember there's no bounds check in and see as discussed before so you do need to be very careful
with that.
Okay let's now try a pointer of pointer.
Okay so what we do to do that is I want you to do.
First you create an integer called Day and then we're going to go integer asterisk p equals ampersand.
And then we're gonna go integer asterisk asterisk P2 equals M P.
So I hope that explains the multiple asterisks now.
So when you have to ask risks it means that it expects you to point to a to a single point a variable.
That's what it means.
And if you had three if you had three of them then you would expect you to point to a uh.
To ask or asks point a variable.
So that that's how that works right.
You see what I mean.
And then what you can do is you can do things like this.
So for example let's say I want to change the value of a from P to I would go ask a ask ask or S P two
equals 50 and then if I go there and compile that.
Oh forgot to print it out guys print f percentile.
And then we're gonna go a and also we're gonna change it from 50 because the value start is fifty six
to twenty five.
And when we do this go Main I says Twenty five.
So can you see how that works.
So when we do one asterisk on P two it takes us to this.
Okay.
It takes us to a variable p here.
And when we do two to ask risks takes the value that P is pointing to and takes us to that which points
to eight.
I knew if it was three asterisks it would be the same it would take us to the second one and then the
third asterisk would take us to the final one.
I just do that quickly just to show you and hopefully three will make it very clear if not send me a
message I'll explain it further so we can go like that.
We can pull that again and you can see that works.
So we go ask a risk ask or ask ask or risk which finally takes us to a C you're probably thinking well
what's the point of having multiple of these.
Well that's a good point for it.
Sometimes you might want to change the value that a point is pointing to for example.
And just to show you how that can work.
I'm going to create another variable and strip B and say say we want to change where P two is pointing
to what we just do P two equals ampersand B and it really is that simple.
Right.
And then if you want to add if you want to change variable b you do ask this asterisk p 2 equals 30.
And then B will become 30 right.
Man 30.
So the great thing here then is that we can actually change P so by doing by doing.
I ask this P two equals M some B integer P.
Here the pointer will no longer point to a low point to be so literally we can also do uh we can also
do this just so I can show you quickly we can do ask or ask P in here
and you should see oh wait a minute.
De de de de de de de.
Oh yeah my mistake.
We also need to change it.
Hey guys so we go ask or ask P two equals ampersand B and then if if we comply then we see 20 because
P now points to be so we just go through this with you because it does get a bit of a bit confusing
doesn't it.
So you go equals 50 B equals 20.
So we create choose two separate variables then we create a point a variable copy that points to A's
memory address okay.
Then we create a double point of value copy 2 that points to the point his memory address and then finally
what we say is what we say get the value of p to go to it because it's a memory address go to it and
set it to the address of B.
Now because P two points to the address of P we said the value of p to the address of B.
Does that make sense.
So then we change where p is pointing to.
So that's the whole point of those double points is because it allows you to change where other points
is appointing two.
So it's quite a handy feature actually in the C programming language.
So that is pointers and memory addresses.
I hope that clears it all up.
It probably is a bit confusing for you just go back through the video watch it again and it'll come
to you.
Takes a while to understand point is I.
Thanks so much.