- I know many see programmers who spend
their careers artfully avoiding pointers,
which is possible, but not smart.
Pointers are puzzling, yet I hope this movie
clears the air for you.
I'll explain what a pointer is, how it's used.
I promise to move nice and slow because
this topic is a very important part
of learning the C language.
The worst thing about pointers is their name.
Pointers, it's descriptive, but a poor choice
because you end up explaining a pointer
by saying a pointer points.
That's pointless.
But I can't change the name so allow me to explain
a pointer by giving you this definition.
A pointer is a variable that holds
a memory location and address.
It's not just any address, you can't say for example,
let's see what's at memory location 96.
Pointers don't work that way.
The address must be the location
of another variable.
Pointer variables are declared similarly
to other variables, although the variable name
is prefixed by the pointer operator, the asterisk.
You'll see an example in a moment.
The pointer variable type matches the type
of variable it references.
For example, an int variable requires an int pointer.
Finally, pointer variables must be initialized
before they're used.
This is true of all variables in the C language,
but especially so for pointers.
Lots of weird errors happen
when a pointer isn't initialized.
I can't repeat it enough.
Pointers must be initialized before they're used.
This code is similar to an exercise
from an earlier movie.
The ampersand operator fetches the address
of the variable pokey.
The percent p placeholder displays that address.
Add an integer pointer variable p.
Remember a pointer is a variable
that holds a memory address.
To get that address you use the ampersand operator
that's already being used in this code.
So you could be able to figure this out on your own,
but just in case I'll initialize the variable
p = &pokey.
This statement assigns the address of variable pokey
to the p pointer variable.
At this point you might be puzzled.
You might say, where's the asterisk in front of the p?
I'll answer that question in a moment.
For now remember that a pointer is a variable
that holds a memory location.
Here that memory location is of the pokey variable.
It's being saved into pointer variable p.
Now duplicate the print f statement.
This time replace &pokey with pointer variable p.
Save these changes.
Build and run.
Both lines output the same address.
One is the address of variable pokey
obtained with the ampersand.
The other is the value saved in pointer variable p,
which is that address.
While the pointer variable is declared
by using an asterisk, it's not always used that way.
Without the asterisk the pointer variable
represents a memory location.
With the asterisk the pointer represents
the value at that location.
This is the dual nature of the pointer variable
and it's something that can be endlessly confusing.
This code is similar to the previous exercise,
but I've initialized the pokey variable
to the value 987.
Two sets of print f statements display the address
and value of the pokey variable.
The first set uses pokey directly.
The second set uses pointer variable p.
In line 14 p is used without the asterisk,
so it's a memory location.
In line 15 p is used with the asterisk,
so it peeks at the contents of that memory location.
Build and run this code.
And the pointer variable p dutifully reports
the correct values, the address, and the contents.
A logical question to ask at this point
is why bother?
I'm serious, at this level using a pointer
is pretty pointless.
You can do many things in C without bothering
with pointers, but they do have their purpose,
and many functions expect you to understand
and know how pointers work.
Later movies in this course also drive home
when and how to use pointers,
so don't give up on them yet.