See some mental model for memory last time and also how to get the address of a variable.
Now I want to talk about pointers so pointers are variables that store the address of other variables.
So here is my mental model for a pointer.
So a pointer is just like any other very well it has an address and that and that value for a pointer
is the address of some other variable.
So the other way I look at it is in the same address but it points to some variable.
That's why we call turret's.
So to actually make a pointer Here's how you do it in each will just make a pointer to an integer and
call my ints pointer
so notice the star.
Here is what really makes it a pointer so points to an integer.
So you can read this from right to left as a pointer to an integer k and we can set its value by using
the address of operator.
So let's make another integer or an actual integer in it 5 and will set the address of x as the value
of the pointer k.
So now the point pointer points to X and its value is the address of x.
So note here that this integer pointer must have the address of an integer.
So it wouldn't work with any other type.
So you might be asking so what.
Why is this useful.
Well I can actually access X through the pointer and have my pointer behave as if it was x.
So C example.
So you can actually say my inch wheelchair
wants to 10 and which is output the value of x.
Alue X is
K and run this and it says the value of x is 10.
But we never set the
value on X directly.
We did it through our my int pointer K and to do this we use what's called the dereference operator.
This star in front of the pointer so when you dereference the pointer the point you're kind of takes
on what it points to.
The dereference operator can be read as value pointed to by.
So the value pointed to by my end pointer is equal to 10.
Sort of how you read that.
So once the pointer is the reference type needs to be known Remember that the type that the pointer
points to is not the type of pointer itself when important new key word that I use with pointers is
called null pointer.
So here if I set this to no pointer
this can actually set in
wintered here.
X just so it doesn't blow up.
So this statement here.
This means that this my end pointer points to nothing.
So null pointer is really a special place in memory that contains nothing.
So one important note is if your pointer is pointing at null pointer dereferencing your pointer like
this.
Let's say my int pointer goes to 10 let's say.
So let's try this.
So it didn't really work.
I think Straten time time is that value.
Negative one.
So it didn't work.
And that is because dereferencing something or when you dereference this pointer you're obtaining the
reference type but no pointer has no type.
It's kind of undefined behavior.
So that's why you're kind of seeing nothing really happening here.
So that's something you have to avoid.
So never do that.
So in the next lecture I'll talk about a similar concept called reference's.