WEBVTT

00:00:00.340 --> 00:00:02.220
Before we move on with the course,

00:00:02.230 --> 00:00:05.870
I want to demonstrate what we talked about in the previous lesson, and I

00:00:05.870 --> 00:00:10.010
will do that by introducing the concept of void pointers.

00:00:10.020 --> 00:00:15.270
So what is a void pointer? In a way, this is the purest kind of a pointer

00:00:15.270 --> 00:00:18.710
because it doesn't have any specific data type attached to it.

00:00:18.720 --> 00:00:21.680
This pointer literally only holds the memory address,

00:00:21.690 --> 00:00:23.760
and there are no strings attached.

00:00:24.340 --> 00:00:26.150
I created two pointers.

00:00:26.160 --> 00:00:30.810
One of them is int, and the other one is void, but they both point

00:00:30.810 --> 00:00:33.960
to the same place in memory, to the variable, x.

00:00:34.640 --> 00:00:37.770
I will print out both of them so that we can verify this

00:00:37.770 --> 00:00:43.350
claim. As you can see, both of the pointers store the same

00:00:43.350 --> 00:00:45.660
memory address, which was expected.

00:00:46.540 --> 00:00:49.660
But what will happen if we try to dereference them?

00:00:50.540 --> 00:00:55.000
You can already see that my editor is warning me, but we'll compile

00:00:55.000 --> 00:00:58.900
it anyway. And it seems like there's an error.

00:00:58.910 --> 00:01:02.770
Void pointer is not a pointer‑to‑object type.

00:01:02.780 --> 00:01:05.860
If you remember the previous lesson, this makes sense.

00:01:06.640 --> 00:01:09.610
When we try to dereference this integer pointer,

00:01:09.620 --> 00:01:13.010
the compiler will know that it should also look at the following three

00:01:13.010 --> 00:01:17.130
bytes in memory because integer values are four bytes long.

00:01:17.160 --> 00:01:20.370
But when we try to dereference a void pointer,

00:01:20.380 --> 00:01:25.260
compiler will produce an error because there are no void variables.

00:01:25.540 --> 00:01:27.360
If you want to dereference pointers,

00:01:27.370 --> 00:01:30.760
the compiler needs to know which data type are they pointing to.

00:01:31.040 --> 00:01:33.950
So, if we cannot dereference void pointers,

00:01:33.960 --> 00:01:37.970
why would we ever use them? Void pointers can be used as a

00:01:37.970 --> 00:01:41.760
placeholder or an interface for a piece of code that we want to

00:01:41.760 --> 00:01:44.900
apply to multiple different types of pointers.

00:01:44.910 --> 00:01:48.100
You will see an example of this in the last lesson of the module.

00:01:48.100 --> 00:01:51.080
We have a memory address of this variable, x,

00:01:51.350 --> 00:01:55.420
which means that we can simply cast this void pointer into an

00:01:55.430 --> 00:01:58.260
integer pointer before dereferencing it.

00:01:59.640 --> 00:02:01.800
And now, if I recompile the program,

00:02:01.810 --> 00:02:04.490
you can see that everything works as expected.

00:02:04.500 --> 00:02:08.259
Now you can see why data types in pointer declarations are

00:02:08.259 --> 00:02:11.620
important. And now that we know all of this,

00:02:11.630 --> 00:02:15.060
we can finally move on to dynamic memory allocation.
