WEBVTT

00:06.000 --> 00:12.630
Her and pointers are actually related and even interchangeable in some situations.

00:12.720 --> 00:24.660
So I had this example so it Gerais virus that's a blank that betray Ramus five elements and it's going

00:24.660 --> 00:30.050
to put them to even numbers 10 that's

00:33.570 --> 00:37.060
and said I had a pointer as well.

00:39.470 --> 00:46.160
So that no point at the start then I'll do something interesting and I'll say the pointer is a signed

00:46.250 --> 00:47.280
array.

00:48.800 --> 01:01.510
That and if I output value pointed to pointer it's

01:04.280 --> 01:17.510
your like that it's outputting the value of the value pointed to by pointer right.

01:17.540 --> 01:23.790
That the reference operator in phi run this.

01:23.790 --> 01:26.280
It says to is kind of interesting.

01:26.560 --> 01:33.370
So notice that I didn't use the ampersand of this variable k it didn't do this.

01:33.730 --> 01:40.080
And that is because an array is really really is kind of like a pointer in the first place.

01:40.090 --> 01:50.590
So it really what this mind does is it says the pointer is assigned the first element pointed to by

01:50.650 --> 01:52.480
Array k.

01:52.570 --> 02:03.330
So it's equivalent to saying this is a address of the first element k.

02:03.550 --> 02:13.060
These are the same I tend to prefer this because this is quite a bit more explicit and it makes quite

02:13.060 --> 02:14.560
a bit more sense.

02:14.560 --> 02:24.620
So let's actually see an example of this and the address element.

02:25.810 --> 02:28.810
Zero is zero would be the first one.

02:28.810 --> 02:32.250
This one is

02:35.070 --> 02:36.480
so I could say this

02:40.260 --> 02:41.880
and I could also say

02:44.150 --> 02:48.490
it's one it is

02:54.180 --> 02:59.920
the address of Element 1 that way.

03:00.370 --> 03:08.990
And if it run that you'll see that these are identical except for the very last part is 1 0 for element

03:09.290 --> 03:09.950
0.

03:10.040 --> 03:10.830
OK.

03:11.120 --> 03:21.980
And for Element 1 it's 4 and that is because it's 4 bytes offset so 4 bytes because an integer is 4

03:21.980 --> 03:25.390
bytes and it's the next one after.

03:25.480 --> 03:25.830
OK.

03:25.880 --> 03:33.910
So this would be kind of this address and this one would be this address.

03:35.640 --> 03:47.280
When using arrays I tend only to use the index square brackets like this instead of using the array

03:47.280 --> 03:49.400
variable itself.

03:49.460 --> 03:59.730
And so I could I could do this and get the same thing I had before.

03:59.800 --> 04:02.680
So it's offset by 4 right.

04:02.770 --> 04:06.880
It's the address of the next element.

04:06.880 --> 04:14.890
But I tend not to do that because again I think this is a bit more understandable and readable even

04:14.890 --> 04:16.430
though it's a bit more typing.

04:16.690 --> 04:16.940
OK

04:20.900 --> 04:31.250
so we can also use pointers just like arrays just like having this I could do something with this pointer

04:31.280 --> 04:34.560
of 1 to 5.

04:34.870 --> 04:36.530
And if I output

04:38.700 --> 04:49.070
array one Hintz the one that

04:51.730 --> 05:00.130
this should have changed because now the pointer is pointing to this element.

05:00.310 --> 05:06.290
This one has the index at one so it's actually pointing to the second element.

05:06.550 --> 05:11.280
So you should see five being output right here.

05:12.370 --> 05:21.820
So I can I can actually use sort of the array syntax for pointers to.

05:21.880 --> 05:24.440
So that's that's another similarity.

05:24.700 --> 05:26.920
Pointers and arrays share.

05:27.580 --> 05:31.810
So let's another example.

05:32.040 --> 05:35.440
Get rid of all this.

05:36.390 --> 05:36.670
That

05:39.540 --> 05:40.010
in.

05:40.020 --> 05:43.030
Say some equals zero.

05:43.130 --> 05:50.120
A Randy the same example we did in the last lecture and or have pointer as well.

05:53.280 --> 05:59.870
Or And we're going to send it to Erie.

05:59.900 --> 06:09.620
So again this makes the grade point to the first element the three and which do this.

06:09.620 --> 06:17.190
And this makes more sense to me and we'll loop through the whole thing.

06:17.630 --> 06:20.610
Zero less and five

06:24.040 --> 06:30.360
plus plus and we'll get some of them together.

06:30.370 --> 06:33.180
But we're going to use the pointer syntax so that

06:37.350 --> 06:42.060
some this some

06:45.950 --> 06:48.440
can run this.

06:48.570 --> 06:56.390
So some 30 K and notice we're using We're only pointing to the array we're not actually using their

06:56.410 --> 07:05.760
aid directly but in fact it actually went through each one using the pointer and summed the value pointed

07:05.760 --> 07:07.690
to by that pointer.

07:08.010 --> 07:12.700
Each iteration they each index.

07:12.850 --> 07:19.180
And there's another way we could do this could do it like this.

07:19.660 --> 07:22.490
Plus plus that.

07:22.500 --> 07:29.360
So you're in that we also get 30 and exact same thing.

07:29.410 --> 07:34.500
So we're getting the value pointed to by pointer each time.

07:34.500 --> 07:37.770
And then we're actually incrementing a pointer.

07:38.110 --> 07:50.150
So the thing to understand about incrementing is that it will move the pointers.

07:50.540 --> 07:57.590
What it's pointing to by the size of the type that it points to in but it's so it's pointing to an integer

07:57.710 --> 08:03.700
array and integers for bytes so it actually will move the address for bytes.

08:03.710 --> 08:05.340
So then will point to this one.

08:05.540 --> 08:08.100
And then this one and then so on.

08:09.100 --> 08:15.420
So in this example we're pointing to each element of the array using the pointer variable.

08:15.700 --> 08:22.800
So in each iteration we point to the element in.

08:22.810 --> 08:31.890
So for example this one is an int so it would move it by 4 bytes that say the array was a char type.

08:32.050 --> 08:35.560
It would only move it by one byte each time.

08:35.670 --> 08:43.230
So they aren't completely interchangeable though you can't actually increment an array for example.

08:43.270 --> 08:50.270
OK that wouldn't work because you'd be changing the where that array points to.

08:50.280 --> 08:59.120
So say I did this here eight plus plus K doesn't actually work

09:02.020 --> 09:05.580
but it would actually be pointing here which is wrong.

09:05.620 --> 09:05.980
Right.

09:05.980 --> 09:15.050
You kind of want it to always stick to to this first element and you always want it to point to this

09:15.070 --> 09:23.110
first element K because otherwise you'd be sort of offset and it would get confusing and you'd get confused

09:23.140 --> 09:24.490
and bad things would happen.

09:25.290 --> 09:25.890
OK.

09:26.450 --> 09:31.730
So we'll see some more similarities in the next lecture.
