In this video, we're going to step through some code that
uses pointers to show you how to best draw diagrams,
to understand what is happening in a line by line fashion.
The first line of code declares a variable X and gives it an initial value of three.
The next line of code declares a variable P that is of type integer pointer.
It does not yet initialize it.
So, in the beginning we have a box,
but we don't yet know what's in the value.
But we do know that the box is called P. The next line of
code will initialize the value of P to have the value of the address of X.
When you see an assignment like this,
it's important to begin by looking at
the left side first and then look at the right side.
Looking at the left side we can see that the value is going to be placed into
the variable P. When we look on the right side of the assignment statement,
we see that the value we're going to place is the address of X.
So, when we execute this line,
we're going to change the variable P to now have an arrow that points to X.
On the next line,
on the left side we see the value that P points to,
which is actually the variable X,
we're going to set that to have the value on the right side which is four.
When we execute this line of code,
we're going to go to that which P points to,
and change that to have the value four.
The next line of code declares a variable Y,
and the value it's initialized to is the value P points to, in this case four.
When we execute this line of code,
we're going to create a new box named Y and give it the value four.
The next line is going to declare a variable of type integer pointer called Q,
and we're going to give it an initial value of the address of Y.
That means we're going to create a new box called Q,
and it's going to have an arrow in it that points to Y.
The next line is a little bit tricky.
On the left, we're going to be storing the value we create into that which Q points to,
namely the box for Y.
And the value that we're going to store there is that which P points to,
which is four plus one.
So, when we execute this line of code we're going to
take the value P points to, which is four,
add one to it, which is five,
and store that at the location Q points to,
namely the variable Y.
The final line of code we have simply gives the value of P to the variable Q. Q was
the receiving value and the value it's going to
get is the value P has, which has an arrow.
So, when we execute this line of code we're going to replace the value
of Q with an arrow that points to the thing P points to.