WEBVTT

00:00:00.340 --> 00:00:05.360
Okay, so now we know what pointers are, but what is the point of pointers?

00:00:05.370 --> 00:00:09.780
The most basic thing you can do with a pointer is to dereference it.

00:00:09.790 --> 00:00:13.620
This is also known as indirection because you are not accessing the

00:00:13.620 --> 00:00:17.530
variable directly, you are getting access through the pointer. To

00:00:17.530 --> 00:00:21.270
dereference a pointer variable, you can just prepend it with this

00:00:21.280 --> 00:00:26.020
asterisk character. In this context, this character is known as

00:00:26.020 --> 00:00:27.560
dereference operator.

00:00:27.570 --> 00:00:31.090
So if I put this operator next to the addr_of_x pointer,

00:00:31.100 --> 00:00:35.400
I'm basically saying that I want to get the value stored at the memory

00:00:35.400 --> 00:00:40.150
address contained in the pointer, the address that this pointer points to.

00:00:40.940 --> 00:00:44.360
In this case, this would resolve to a number 2.

00:00:45.040 --> 00:00:49.260
We are accessing the value from the variable x through the pointer.

00:00:50.440 --> 00:00:54.020
So basically, when you add this dereference operator to the pointer,

00:00:54.030 --> 00:00:57.550
you can use this pointer just like a variable that it points to.

00:00:58.340 --> 00:01:01.200
If you put this on the left side of the assignment,

00:01:01.210 --> 00:01:04.300
you can update the value of the pointed place in memory.

00:01:04.360 --> 00:01:09.760
So if I assign 5, this will update the value of the variable x to 5.

00:01:10.440 --> 00:01:14.420
We should not confuse this operator with the asterisk we use to declare the

00:01:14.420 --> 00:01:19.170
variable as a pointer. Only in the case of pointer declaration this character

00:01:19.170 --> 00:01:23.890
is declaring this variable as a pointer. In any other context, when this

00:01:23.890 --> 00:01:26.000
character is next to the pointer variable,

00:01:26.010 --> 00:01:29.130
it is used to the reference it so that we can read and write

00:01:29.130 --> 00:01:31.560
to the memory address that it contains.

00:01:33.840 --> 00:01:37.650
I wrote this simple program as an exercise so that you can use it

00:01:37.660 --> 00:01:40.320
to see if you understand how pointers work.

00:01:40.340 --> 00:01:43.450
You can pause the video and try to guess what will be

00:01:43.450 --> 00:01:46.510
printed out from each one of these statements and then

00:01:46.510 --> 00:01:48.560
come back to check if you're right.

00:01:49.540 --> 00:01:53.870
Of course, I could have made this harder by using the variable x in the same

00:01:53.870 --> 00:01:58.610
statement where I dereference the pointer which points to the variable x, but

00:01:58.610 --> 00:02:01.260
the point of this exercise is not to trick you.

00:02:01.840 --> 00:02:04.060
Okay, let's quickly go through this code.

00:02:04.640 --> 00:02:06.720
These first two lines are easy.

00:02:06.920 --> 00:02:10.460
We are just printing out the values stored in each variable.

00:02:11.340 --> 00:02:16.740
The third line is dereferencing the pointer p. Since p is pointing to

00:02:16.740 --> 00:02:20.980
the address of variable x, that means that the value printed out here

00:02:20.980 --> 00:02:24.450
will be the value stored in x, which is 3.

00:02:25.440 --> 00:02:26.760
Let's go to the next line.

00:02:27.340 --> 00:02:31.260
This expression, in case you didn't know, is the same as this one.

00:02:32.340 --> 00:02:35.540
You are adding something to the value from this variable and

00:02:35.540 --> 00:02:38.460
then storing that result to the same variable.

00:02:39.240 --> 00:02:44.640
The right side of the assignment always gets executed first. Dereferenced

00:02:44.640 --> 00:02:49.740
pointer p will get the value from the variable x, which is 3, and it will

00:02:49.750 --> 00:02:52.360
add it to the value from the variable y,

00:02:52.370 --> 00:02:58.240
which is 5. Five plus three is eight, and this value gets stored

00:02:58.250 --> 00:03:01.750
inside of the dereferenced pointer, which is x.

00:03:03.240 --> 00:03:06.860
So the variable x is now holding the number 8.

00:03:07.740 --> 00:03:11.620
Then we are reassigning this pointer to the address of the

00:03:11.620 --> 00:03:15.180
variable y. In this case, we are not changing the value

00:03:15.180 --> 00:03:16.650
that this pointer points to.

00:03:16.660 --> 00:03:19.660
We are changing the value from the pointer itself,

00:03:19.670 --> 00:03:21.860
the memory address it contains.

00:03:22.540 --> 00:03:26.630
And now in this expression, the dereferenced pointer is the

00:03:26.630 --> 00:03:33.000
value from y, which is 5. Five plus 5 is 10, and we store that

00:03:33.000 --> 00:03:35.150
number inside of the variable y.

00:03:36.340 --> 00:03:42.900
So y will now be 10, x will be 8, and the dereferenced pointer will also be

00:03:42.900 --> 00:03:50.130
10 because this is basically the same thing as writing y here. Let's compile

00:03:50.130 --> 00:03:52.560
this and make sure that we did everything right.

00:03:53.640 --> 00:03:58.500
And it seems like we did. You can make your own small exercises

00:03:58.500 --> 00:04:01.950
like this one and move things around just so that you can see if

00:04:01.950 --> 00:04:05.840
you understand what is going on. Now we know how pointers work, but

00:04:05.840 --> 00:04:07.530
with this dereference operator,

00:04:07.530 --> 00:04:11.010
it seems that their only purpose is to indirectly access

00:04:11.010 --> 00:04:14.730
some other variable, like an alias, two variables that can

00:04:14.730 --> 00:04:16.459
access the same data in memory.

00:04:16.740 --> 00:04:20.440
And this might not seem like something useful, but to really see

00:04:20.440 --> 00:04:24.120
how essential this is in programming, we need to first cover one

00:04:24.120 --> 00:04:26.430
more thing, and that is scope.

00:04:26.440 --> 00:04:27.960
I'll see you in the next lesson.
