WEBVTT

00:00:01.040 --> 00:00:04.380
If only the first dimension is dynamic and the rest of them can

00:00:04.380 --> 00:00:07.740
be static, there is a better way to create a multidimensional

00:00:07.740 --> 00:00:11.980
array on the heap with the good, old two‑square‑bracket syntax.

00:00:11.980 --> 00:00:14.060
But before I show you,

00:00:14.060 --> 00:00:18.250
I want to first clarify the difference between specific types of pointers.

00:00:18.640 --> 00:00:23.330
This is a pointer to an integer which points to the first of 4 bytes

00:00:23.330 --> 00:00:27.620
which make an integer, so when we dereference this pointer we will

00:00:27.620 --> 00:00:31.570
get an integer, and the pointer arithmetic of this pointer will work

00:00:31.580 --> 00:00:33.960
in the increments of 4 bytes.

00:00:34.740 --> 00:00:39.550
And now, what if I had an array of integers and I pointed this pointer

00:00:39.550 --> 00:00:43.130
to this array? This is still a pointer to an integer.

00:00:43.140 --> 00:00:46.490
It points to the first integer from this array.

00:00:46.500 --> 00:00:50.220
It has no idea that this is an array, but we can still use it as an

00:00:50.220 --> 00:00:52.850
array name because of the pointer arithmetic.

00:00:54.040 --> 00:00:57.760
So, now that we made this clear, what does this line do?

00:00:58.540 --> 00:01:02.860
This line will create an array of three pointers to integers.

00:01:03.640 --> 00:01:08.050
It will maybe look clearer if I move this asterisk next to the data type.

00:01:09.240 --> 00:01:14.480
But what if I put this identifier inside of parentheses together with

00:01:14.480 --> 00:01:18.510
the asterisk? This is no longer an array declaration.

00:01:18.520 --> 00:01:22.000
This is a pointer which points to an array of three

00:01:22.000 --> 00:01:26.980
integers, so this is truly a pointer to an array, not a

00:01:26.980 --> 00:01:29.460
pointer to the first element of the array.

00:01:30.540 --> 00:01:33.290
Since it points to the array with three integers,

00:01:33.300 --> 00:01:37.340
that means that its pointer arithmetic works in increments of 12

00:01:37.340 --> 00:01:41.460
bytes, because 3 integers need 12 bytes in total.

00:01:42.540 --> 00:01:43.030
Okay,

00:01:43.040 --> 00:01:47.550
this kind of makes sense, but what will we get when we dereference this pointer?

00:01:48.340 --> 00:01:49.030
Well,

00:01:49.040 --> 00:01:52.790
if a dereferenced pointer to an integer gives us an integer, a

00:01:52.790 --> 00:01:57.370
dereferenced pointer to an array should give us an array. And this is

00:01:57.370 --> 00:02:01.660
exactly what it does, but since every array name is just a pointer to

00:02:01.660 --> 00:02:03.230
the first element of the array,

00:02:03.240 --> 00:02:06.040
you can think of the result of this expression as a

00:02:06.040 --> 00:02:08.860
pointer to the first element of the array.

00:02:10.240 --> 00:02:13.790
I hope that this makes sense, because I get tangled up in trying to explain

00:02:13.790 --> 00:02:18.350
this terminology of a pointer to an array, because we would sometimes say that

00:02:18.350 --> 00:02:22.050
the pointer is pointing to an array even though we mean the pointer is

00:02:22.050 --> 00:02:24.760
pointing to the first element of that array.

00:02:25.740 --> 00:02:30.360
If you didn't get this, maybe it will become clearer once I use it in practice.

00:02:30.740 --> 00:02:34.500
So, let's say that we need a dynamic array where only the first

00:02:34.500 --> 00:02:39.080
dimension is dynamic. In that case, new operator allows us to

00:02:39.080 --> 00:02:42.060
allocate multidimensional arrays on the heap.

00:02:43.040 --> 00:02:46.910
I could store the memory address from the heap in a simple pointer, but

00:02:46.910 --> 00:02:50.060
I want to store it inside of a pointer to an array.

00:02:50.440 --> 00:02:54.940
Specifically, I want a pointer to an array of three integers, because the

00:02:54.940 --> 00:02:58.360
second static dimension is the array of three integers.

00:02:59.340 --> 00:03:04.860
And now I can use this pointer to access each row by using an offset.

00:03:05.840 --> 00:03:09.520
Since this is a pointer to an array of three integers, and three integers

00:03:09.520 --> 00:03:13.740
need 12 bytes, the pointer arithmetic of this pointer will work with an

00:03:13.740 --> 00:03:19.700
offset of 12 bytes, so adding 1 to this pointer will get us an address of

00:03:19.700 --> 00:03:22.250
the first element from the second row.

00:03:22.740 --> 00:03:26.500
Once we have that, we can use an offset of one integer to get

00:03:26.500 --> 00:03:29.250
the wanted number from the specific column.

00:03:29.640 --> 00:03:32.360
This syntax should look familiar by now.

00:03:32.370 --> 00:03:36.560
You know that we can rewrite this to actually use two square brackets.

00:03:37.440 --> 00:03:41.760
So now we have all of the elements placed in the same block of memory, and we

00:03:41.760 --> 00:03:45.450
can also use the two square bracket syntax to access them.

00:03:45.840 --> 00:03:49.000
But remember that this is only possible if the first

00:03:49.000 --> 00:03:52.050
dimension is dynamic and others are static.

00:03:52.440 --> 00:03:55.290
If I wanted to have another static dimension,

00:03:55.300 --> 00:03:59.830
I can just add it to this declaration. And now I should have a pointer to

00:03:59.830 --> 00:04:03.360
the array that contains all of these static dimensions.

00:04:04.040 --> 00:04:06.220
I can prove this by using auto.

00:04:06.230 --> 00:04:10.050
If I use the auto keyword here and hover over this identifier,

00:04:10.060 --> 00:04:13.550
you can see the type of the pointer that my editor deducted.

00:04:14.640 --> 00:04:18.600
So, to sum up, if you need a static multidimensional array,

00:04:18.610 --> 00:04:23.440
use the regular syntax. If you need all of the dimensions to be dynamic,

00:04:23.440 --> 00:04:27.300
make a simple array and use the formula to manually access elements.

00:04:27.310 --> 00:04:30.460
And if only the first dimension needs to be dynamic,

00:04:30.470 --> 00:04:34.300
you can create a multidimensional array on the heap and use auto to

00:04:34.300 --> 00:04:37.050
deduce the correct type of pointer to an array.
