Okay, so now we know what pointers are, but what is the point of pointers?
The most basic thing you can do with a pointer is to dereference it.
This is also known as indirection because you are not accessing the
variable directly, you are getting access through the pointer. To
dereference a pointer variable, you can just prepend it with this
asterisk character. In this context, this character is known as
dereference operator.
So if I put this operator next to the addr_of_x pointer,
I'm basically saying that I want to get the value stored at the memory
address contained in the pointer, the address that this pointer points to.
In this case, this would resolve to a number 2.
We are accessing the value from the variable x through the pointer.
So basically, when you add this dereference operator to the pointer,
you can use this pointer just like a variable that it points to.
If you put this on the left side of the assignment,
you can update the value of the pointed place in memory.
So if I assign 5, this will update the value of the variable x to 5.
We should not confuse this operator with the asterisk we use to declare the
variable as a pointer. Only in the case of pointer declaration this character
is declaring this variable as a pointer. In any other context, when this
character is next to the pointer variable,
it is used to the reference it so that we can read and write
to the memory address that it contains.
I wrote this simple program as an exercise so that you can use it
to see if you understand how pointers work.
You can pause the video and try to guess what will be
printed out from each one of these statements and then
come back to check if you're right.
Of course, I could have made this harder by using the variable x in the same
statement where I dereference the pointer which points to the variable x, but
the point of this exercise is not to trick you.
Okay, let's quickly go through this code.
These first two lines are easy.
We are just printing out the values stored in each variable.
The third line is dereferencing the pointer p. Since p is pointing to
the address of variable x, that means that the value printed out here
will be the value stored in x, which is 3.
Let's go to the next line.
This expression, in case you didn't know, is the same as this one.
You are adding something to the value from this variable and
then storing that result to the same variable.
The right side of the assignment always gets executed first. Dereferenced
pointer p will get the value from the variable x, which is 3, and it will
add it to the value from the variable y,
which is 5. Five plus three is eight, and this value gets stored
inside of the dereferenced pointer, which is x.
So the variable x is now holding the number 8.
Then we are reassigning this pointer to the address of the
variable y. In this case, we are not changing the value
that this pointer points to.
We are changing the value from the pointer itself,
the memory address it contains.
And now in this expression, the dereferenced pointer is the
value from y, which is 5. Five plus 5 is 10, and we store that
number inside of the variable y.
So y will now be 10, x will be 8, and the dereferenced pointer will also be
10 because this is basically the same thing as writing y here. Let's compile
this and make sure that we did everything right.
And it seems like we did. You can make your own small exercises
like this one and move things around just so that you can see if
you understand what is going on. Now we know how pointers work, but
with this dereference operator,
it seems that their only purpose is to indirectly access
some other variable, like an alias, two variables that can
access the same data in memory.
And this might not seem like something useful, but to really see
how essential this is in programming, we need to first cover one
more thing, and that is scope.
I'll see you in the next lesson.