(classical music)
- [Narrator] A pointer is a variable in C++
that contains the address of a memory location.
Have you ever been to your local post office
where you've seen a row of P.O. boxes?
Or maybe even a row of mailboxes for an apartment complex.
This always reminds me
of how data is stored in the computer.
Each mailbox has an address
and inside there might be a letter.
In our case, each address might contain data.
The next mailbox is usually numbered in succession.
When data is stored in memory on the computer,
the location of the memory is called an address.
I think this is why I think of a post office.
The memory locations are numbered sequentially
using a hexadecimal number to represent that address.
For example, this address is 01AC4D
where the 0X at the beginning just indicates
that it's a hexadecimal number.
It is important to understand that these addresses
can vary from execution to execution.
A pointer is a variable that can store an address location.
Why use pointers at all?
Well a few reasons include the fact that pointer notation
can be faster when working with arrays,
they are used extensively to provide a function
with access to large blocks of data
such as arrays that are defined outside the function.
An important use is the ability to allocate memory
for new variables dynamically or on the fly if you will
using a pointer to keep track of the memory address.
When a pointer is not initialized,
it will automatically be set to no pointer
or to the keyword null.
This sets the pointer to all zeroes.
It is good programming practice
to always initialize a pointer.
Note, when creating a variable name,
it's helpful to use a prefix of P
to indicate the value is a pointer,
but this is not required.
Pointers actually is a data type
that contains the address of another piece of data.
To obtain the address of any variable in C++,
we can use the ampersand symbol.
The ampersand symbol will allow us to get the address
of the piece of data.
Remember, the address changes
every time we execute the code.
This ampersand or address of operator
is a unary operator that obtains the address of a variable.
Let's walk through this example that I have set up here.
Starting on line 10, I'm declaring an integer variable
called number and setting it to a random value of 240.
On line 11, I'm declaring my first pointer.
I know it's a pointer because I have an asterisk
after the data type.
I also notice that the data type of my pointer
is the same as the data type of my variable name.
That has to be the case.
I cannot store the address of a double variable
into an integer pointer.
They have to be the same data type.
Before we actually run the program,
I do wanna show you also that the asterisk
can be located either right after the data type
or right before the name of the variable.
Both of those are the same in C++
so it really is a preference on your part.
I usually put the asterisk after the data type
and before the name of my pointer.
So on line 11 is our first pointer.
It's an integer pointer and it's called numPtr.
Then on line 12, I'm using the address of operator,
the ampersand, to grab the address
of the variable called number
and to put that into my integer pointer called numPtr.
And on line 13, I'm using the cout command
to print out the address.
Now since I have numPtr as the variable to be printed out,
the actual variable will print out the address
of where the variable number is located in memory.
Now that I said all that,
let's go ahead and run this and take a look.
I'm gonna use the shortcut keys
or Control + F5 to run my program.
Alright it says here that the address
of the variable number is 00FEF8F8.
That's the hexadecimal value of the memory location
for the variable number and if we run it again,
we will get a different address.
This time I got B3FE88.
And each time I run it I would get
whatever address happened to be available
at the time of running my program.
So now we've seen how to define a pointer
and how to assign a value to a pointer.
The pointer isn't just a pointer to an address.
It's a pointer to a particular type of data item
at that address.
So if we wanted to find another variable of type double
and then try to use our pointer to get the address,
you'll see we're gonna get an error. Let's try it.
I'm gonna declare a double variable called number2
and I'll just set it to 13.5
and now I'm gonna try and assign the address
of this number using the ampersand to my integer pointer
and you'll notice when I do that,
I have a red squiggly under the equals sign.
Let's mouse over and see what the error is.
The error says "a value of type double*,"
meaning the pointer to a double,
"cannot be assigned to an integer pointer."
So as we said, we cannot reassign the address for a double
to an integer pointer.
Okay, I'm gonna take these two back out again.
So as you can see,
the address and the pointer go hand in hand.
The address represents the location of the data in memory.
It's a hexadecimal number.
The pointer contains that address value.
The pointer actually contains a hexadecimal number.
I'd like to talk a little bit
about how the memory allocation works within C++
for the different data types.
The amount of memory allocated for a variable
depends on the data type of the variable.
Let's take a look at a chart
that shows how much memory is allocated
for each of the different C++ data types.
For example, a character variable would take up one byte
versus a double which would take up eight bytes.
So in this chart it says character and bool are one byte,
short is two bytes, int, long and float are four bytes
and double is eight bytes.
This will especially important
when we use pointers that are pointing to arrays of data
because we'll need to understand
what is the size of each of the values inside the array.
For now, let's which over to Visual Studio
and take a look at the sample program
that I already have written.
On line 10, I'm printing out the size of a Boolean variable
just to show you that the chart that we had
matches the fact that Boolean is one byte, et cetera.
On line 17, I'm creating three different pointer variables.
Line 17 is a Boolean pointer
and I've given that variable name pBool.
Then I have a character pointer on line 18
and an integer pointer on line 19.
It's important to distinguish between
the size of the data that the pointer is referencing
and the size of the pointer variable itself.
Remember, the pointer holds the address of the data
so the address size is always only gonna need
four bytes worth of memory,
but the pointer does know the type of data it is referencing
so it knows how much space the computer is allocating
based on the data type that it's pointing to.
Let's run this program and take a look at the results.
As you can see, the first few values
show us what we already know.
Boolean is one byte, character is one byte,
int, float and long are four bytes
and a double is eight byte.
A Boolean pointer as well as a character pointer
and int pointer and any other pointers are all four bytes
again, because the only data that a pointer data type has
is an address which is a hexadecimal value
pointing to an address in memory.
I do wanna emphasize that when you're declaring
a new pointer to a variable that the data types must match.
So if you were creating a Boolean variable
and you wanted to store the address
of that variable in a pointer,
you need to create a Boolean pointer.
(classical music)