WEBVTT

00:00.750 --> 00:05.120
I said earlier that are pointers and arrays almost work interchangeably.

00:05.380 --> 00:13.320
Well that only applies to one dimensional or it is with pointers pointers with multi-dimensional arrays

00:13.370 --> 00:17.430
is a little bit different than using pointers with one dimensional right.

00:17.760 --> 00:33.720
So say we had this array A to the Marybel last time you this well you'd think you could do something

00:33.720 --> 00:40.810
with this.

00:41.150 --> 00:43.250
And unfortunately you can't.

00:43.250 --> 00:47.750
So you think you could do this because there's you know one dimension here in one dimension and there

00:47.750 --> 00:55.160
and maybe you know if you had one pointer and another pointer kind of in between here that would kind

00:55.160 --> 00:58.720
of compensates for this one dimension the other dimension.

00:59.000 --> 01:02.280
Well unfortunately this doesn't work right.

01:02.330 --> 01:08.610
So an error you can't initialize that variable with the array.

01:08.760 --> 01:15.000
So it's saying so how would we have a pointer to a 2D array.

01:15.030 --> 01:18.240
Well we could do this instead.

01:20.340 --> 01:21.160
When juror

01:24.860 --> 01:32.820
this and have B 10 here is there this needs to be the same way.

01:33.730 --> 01:37.690
And you could just do that k.

01:37.710 --> 01:43.900
So what this is is a pointer to an array of 10 items.

01:44.490 --> 01:46.970
It does exactly what you want.

01:46.980 --> 01:51.360
So let's see an example for example.

01:51.420 --> 01:54.410
So let's say we had constant max.

01:54.500 --> 01:55.960
Call it 10 here.

01:59.210 --> 02:00.170
Max

02:04.110 --> 02:04.740
it's like that

02:07.390 --> 02:15.280
and sleep over all the rows.

02:17.930 --> 02:20.600
In all columns

02:28.090 --> 02:33.730
in just say pointer are in.

02:34.040 --> 02:40.590
King can do this because again sort of work like arrays at least in this dimension.

02:40.590 --> 02:41.410
Right.

02:42.000 --> 02:45.330
And of course we have an array of them so we can use.

02:45.330 --> 02:46.890
See here.

02:47.370 --> 02:56.730
This doesn't matter too much or it's a sign and some values and just output those values are and who

02:56.730 --> 02:59.680
are.

03:01.640 --> 03:02.660
C.

03:02.850 --> 03:04.500
Good to see

03:09.620 --> 03:20.620
you pointer of R and C.

03:20.770 --> 03:25.430
So if you're in this should see actually work.

03:25.480 --> 03:32.730
So are using that pointer to modify the to the array.

03:32.730 --> 03:33.000
Right.

03:33.070 --> 03:42.500
So you go through the first row and then we just give its values and the second Aronne its values right.

03:42.970 --> 03:50.780
And so on and so you can do this as well.

03:50.850 --> 03:51.910
Dynamic memory.

03:51.930 --> 03:54.260
And in fact in a couple of different ways.

03:54.280 --> 04:10.040
So the first way I'll show you is say we don't need this anymore and we can say this new and in Max

04:11.510 --> 04:14.780
and x a.

04:14.830 --> 04:22.660
So this will actually create a contiguous to the array in memory and has pointer point to it.

04:23.200 --> 04:31.630
And just like normal we can do is delete this pointer.

04:31.880 --> 04:38.660
So this is exactly the same and it works at the same see the same result.

04:41.620 --> 04:49.710
Well let me show you one more way you could create a two tier and then is using an int pointer pointer

04:49.710 --> 04:51.750
that I showed before.

04:52.040 --> 04:58.720
And so now you're going to see why we couldn't do the end pointer pointer before.

04:58.940 --> 05:08.100
So it's a pointer this and watch say new

05:11.530 --> 05:22.310
winter or re k so this creates a new pointer and you're wondering like what kind of direction that R8

05:22.310 --> 05:23.040
goes.

05:23.050 --> 05:27.460
Well in this case you can kind of think of it as it goes sort of down.

05:27.530 --> 05:33.110
So it's kind of this first column but it's the first column of pointer's.

05:33.220 --> 05:36.880
So it's an array of pointers.

05:36.900 --> 05:54.790
So then how we set all the arrays on it is this the loop or are equals zero AR's less than max are A-plus

05:54.830 --> 05:57.050
plus.

05:57.580 --> 06:08.540
And the pointer of our equals new int max.

06:08.850 --> 06:18.560
So each of these pointers now will be assigned an array of this Max like

06:21.530 --> 06:28.670
so now you can use it just like you did for an.

06:28.720 --> 06:31.870
So you can

06:48.180 --> 06:49.180
we help

06:53.010 --> 06:55.540
can see in kind of use of like before

06:58.040 --> 07:02.100
and but we haven't cleaned it up yet.

07:02.180 --> 07:02.840
OK.

07:03.230 --> 07:05.360
So notice that we had

07:08.000 --> 07:17.640
so that we made kind of the sort of big column of integer pointer or like that array of integer pointers.

07:17.960 --> 07:19.160
This line here.

07:19.370 --> 07:23.090
And then we kind of made all the arrays after for each row.

07:23.420 --> 07:29.170
Well we have to do that in reverse if you want to clean it up so we have to clean up all the rows first.

07:31.160 --> 07:32.080
Yes.

07:35.880 --> 07:40.610
So those leaks are

07:42.920 --> 07:47.410
or and then we delete

07:50.120 --> 08:02.200
the pointer K so that it does everything in reverse deletes all the rows first but we made all the rows

08:02.200 --> 08:03.180
second.

08:04.000 --> 08:08.780
And they made this pointer first and then we have to delete the pointer last.

08:09.050 --> 08:12.600
So it kind of goes in reverse order to its creation.

08:13.670 --> 08:18.900
So what is the difference between the two.

08:19.120 --> 08:23.950
Well this what we have for ain't

08:26.980 --> 08:28.810
for Star pointer.

08:31.560 --> 08:37.140
Like that's new in x.

08:37.200 --> 08:41.280
Max Right.

08:41.320 --> 08:44.710
This one is contiguous in memory.

08:44.770 --> 08:45.320
Right.

08:45.430 --> 08:49.330
So in memory they actually are all next to each other.

08:50.320 --> 08:59.800
Unlike this one which is possibly piecemeal in terms of the memory out because we don't know what addresses

09:00.160 --> 09:03.520
the new operator will return to us.

09:03.520 --> 09:06.040
They might be contiguous but maybe they're not.

09:06.100 --> 09:07.590
So we don't know.

09:07.870 --> 09:09.520
And when you use them.

09:09.520 --> 09:17.650
Well if you want something pretty flexible in terms of the row and column sizes you should use the second

09:17.650 --> 09:23.120
one this in your pointer method for that case.

09:23.200 --> 09:29.110
But if you know the size or you can sort of calculate the size somehow then you should probably just

09:29.110 --> 09:33.380
use this sort of contiguous one because this is usually more efficient.

09:33.590 --> 09:34.660
OK.

09:35.170 --> 09:44.740
So we'll talk more about pointer pointer and just pointers to pointers more in the next lecture.
