- [Instructor] Pointers are a very powerful,
useful, dangerous, and common data type.
To really understand pointers,
you need to first understand how a variable works.
A variable is a typed and named location in memory.
This is a variable definition.
Here, memory is allocated for a value of type integer
and is associated with the name x.
Here, the integer value one
is copied into the memory location
associated with the integer variable x.
This is both a definition and an assignment.
Memory is allocated for an integer,
and the value from the variable named x
is copied into the variable named y.
So the variable named y now contains a separate integer
in a separate memory location
with the same value as the variable x.
So we can see that the name of a variable
is used as an index to map to a memory address
and a particular associated data type.
C++ also provides a pointer type
that is a pointer to a value,
as opposed to carrying the value itself.
This is a pointer definition.
It's a variable named ip of the type pointer to int.
So memory is allocated for a value of type pointer.
The pointer is also strongly typed.
That is, the compiler associates this pointer
with the int type.
So we call this an integer pointer.
It is a pointer to an integer value.
Here, the address of the integer variable named x
is placed in the integer pointer variable named ip.
The ampersand operator
is formally called the reference operator,
but in this context,
it's more commonly called the address of operator.
It returns the address of an object
suitable for assigning to a pointer.
So this statement assigns the address of x
to the integer pointer ip.
The integer pointer ip now points to the integer variable x.
It's worth noting that the ampersand operator
is also used in C++ for a special reference type
that will be covered later.
This assignment statement copies the value pointed to by ip,
which currently points the integer variable x
into the variable y.
In this context, the asterisk
is called the pointer dereference operator.
It's used to get at the value pointed to by a pointer.
So this expression dereferences the integer pointer ip,
grabs the value, and copies that value into the variable y.
You can of course change ip
to point to a different variable.
So let's take a look at a simple example.
Here I have a working copy of pointers.cpp
from the chapter two exercise files.
And you see we have an integer x with a value of seven,
an integer y with a value of 42,
and an integer pointer ip that is initialized
with the address of the integer variable x.
And so we print these values.
And you'll notice here in line 13,
we're dereferencing ip to print the value pointed at by it,
which in this case is going to give us the value of x.
So when I build and run this,
you see we get these three printf statements.
And we have the value of x is seven,
the value of y is 42,
and the value pointed at by ip is seven,
which is the value of x.
Now, I can come down here, and I can change the value of x
to, say, 73,
and then print this dereferencing of the pointer again.
And you'll notice that now the value
of what's pointed at by ip,
it's still a value of x, but x is changed.
And in fact, I can make another copy of this,
and I can change instead ip
to point at y instead of pointing at x.
And now when I build and run,
you'll see the value of what's pointed at by ip
is now the value of y instead of the value of x.
So here I've shown you the fundamentals of pointers.
Understanding pointers is critical
to using C++ effectively.
So take some time and experiment with pointers on your own,
understanding this will serve you well for the long-term.
Pointers are also essential for understanding references,
which we'll discuss in the next lesson.
You'll see many more examples of pointers
in the rest of this course.
So I'm going to leave this working file open
for the next lesson
because we're going to build on this knowledge
to understand references.