WEBVTT

00:00.830 --> 00:07.660
See some mental model for memory last time and also how to get the address of a variable.

00:07.780 --> 00:17.070
Now I want to talk about pointers so pointers are variables that store the address of other variables.

00:17.080 --> 00:21.210
So here is my mental model for a pointer.

00:21.640 --> 00:31.840
So a pointer is just like any other very well it has an address and that and that value for a pointer

00:31.840 --> 00:36.240
is the address of some other variable.

00:36.280 --> 00:44.010
So the other way I look at it is in the same address but it points to some variable.

00:44.620 --> 00:47.590
That's why we call turret's.

00:47.620 --> 00:57.470
So to actually make a pointer Here's how you do it in each will just make a pointer to an integer and

00:57.560 --> 00:59.710
call my ints pointer

01:02.950 --> 01:04.690
so notice the star.

01:04.690 --> 01:10.010
Here is what really makes it a pointer so points to an integer.

01:10.030 --> 01:18.330
So you can read this from right to left as a pointer to an integer k and we can set its value by using

01:18.330 --> 01:19.930
the address of operator.

01:19.950 --> 01:34.430
So let's make another integer or an actual integer in it 5 and will set the address of x as the value

01:34.430 --> 01:38.020
of the pointer k.

01:38.470 --> 01:45.910
So now the point pointer points to X and its value is the address of x.

01:45.970 --> 01:52.570
So note here that this integer pointer must have the address of an integer.

01:52.600 --> 01:55.720
So it wouldn't work with any other type.

01:55.830 --> 01:58.400
So you might be asking so what.

01:58.410 --> 01:59.990
Why is this useful.

02:00.360 --> 02:10.400
Well I can actually access X through the pointer and have my pointer behave as if it was x.

02:10.410 --> 02:13.510
So C example.

02:13.510 --> 02:19.550
So you can actually say my inch wheelchair

02:24.480 --> 02:29.850
wants to 10 and which is output the value of x.

02:31.110 --> 02:35.330
Alue X is

02:40.180 --> 02:47.210
K and run this and it says the value of x is 10.

02:47.400 --> 02:48.900
But we never set the

02:51.820 --> 02:54.720
value on X directly.

02:54.790 --> 03:04.190
We did it through our my int pointer K and to do this we use what's called the dereference operator.

03:04.200 --> 03:13.110
This star in front of the pointer so when you dereference the pointer the point you're kind of takes

03:13.110 --> 03:15.340
on what it points to.

03:16.230 --> 03:21.630
The dereference operator can be read as value pointed to by.

03:22.110 --> 03:29.820
So the value pointed to by my end pointer is equal to 10.

03:29.960 --> 03:32.430
Sort of how you read that.

03:32.450 --> 03:40.250
So once the pointer is the reference type needs to be known Remember that the type that the pointer

03:40.250 --> 03:49.280
points to is not the type of pointer itself when important new key word that I use with pointers is

03:49.280 --> 03:51.420
called null pointer.

03:51.800 --> 03:58.250
So here if I set this to no pointer

04:02.460 --> 04:07.260
this can actually set in

04:09.770 --> 04:11.200
wintered here.

04:13.240 --> 04:18.870
X just so it doesn't blow up.

04:19.000 --> 04:24.590
So this statement here.

04:25.000 --> 04:30.690
This means that this my end pointer points to nothing.

04:31.000 --> 04:38.740
So null pointer is really a special place in memory that contains nothing.

04:38.750 --> 04:47.600
So one important note is if your pointer is pointing at null pointer dereferencing your pointer like

04:47.630 --> 04:48.500
this.

04:48.500 --> 04:55.030
Let's say my int pointer goes to 10 let's say.

04:55.170 --> 04:59.310
So let's try this.

04:59.370 --> 05:02.170
So it didn't really work.

05:02.210 --> 05:07.690
I think Straten time time is that value.

05:07.690 --> 05:09.050
Negative one.

05:09.280 --> 05:10.560
So it didn't work.

05:11.100 --> 05:21.190
And that is because dereferencing something or when you dereference this pointer you're obtaining the

05:21.190 --> 05:25.750
reference type but no pointer has no type.

05:25.840 --> 05:27.790
It's kind of undefined behavior.

05:27.790 --> 05:31.740
So that's why you're kind of seeing nothing really happening here.

05:32.260 --> 05:34.660
So that's something you have to avoid.

05:34.660 --> 05:36.030
So never do that.

05:37.380 --> 05:41.970
So in the next lecture I'll talk about a similar concept called reference's.
