- In this video, we are continuing
our discussion on pointers.
In particular, how to declare and initialize
a pointer variable.
When you declare a variable,
you typically say, for example,
int size and we assign the value 15.
To declare an int pointer,
we simply put a * in front of the pointer variable.
Now, we have two types of declaration.
One for the regular variable of type int
and the other, of a pointer,
meaning a variable that stores an address of size int.
That pointer doesn't store anything yet.
It's simply capable of storing an address of type int.
Now, we can actually assign a value to the pointer int
and we assign the value of the address of size.
Size is an integer whose value is 15.
Now, the pointer stores the actual location in memory
of that variable size.
To do so, we use the & sign
just to the left of the variable name.
So, we have three things going on here.
Let's see the same thing in code.
The same exact thing, line 20,
we have now assigned the address of the variable size
to the pointer named pointerInt.
If you recall, all memory in the computer
is labeled by hex numbers or binary,
depending of what mechanism you use to access them.
When you store a value in a variable,
essentially you are picking one of those
hexadecimal labels of memory locations
and store some data in it.
The pointer gives you a mechanism
to refer directly to that address in memory.
However, you don't have to know the hex value,
although you could.
But, it simplifies the memory management
by using the variable
and its address through the &.
One thing to note is not to be confused by the semantics.
When you use the pointer itself,
you use the *.
When you use the address, you use the &.
We will see more how we can use pointers and addresses
they point to in the upcoming videos.