Lesson 11: Pointers
In this lesson you will learn:
- how to use pointers
- what pointers are
- how to declare pointers
- the meaning of the ampersand& symbol
- the meaning of the asterisk * symbol
Q: What is the difference between a blonde and an inflatable
doll?
A: About 2 cans of hair spray
The C language takes a lot of flack for its ability to
peek and poke directly into memory. This gives great flexibility
and power to the language, but it also makes it one of the
great hurdles that the beginner must have in using the language.
. With that said, let's get started.
All variables obviously have to be stored into memory, but
where are they stored? Imagine memory as this big long street
with houses on it. Each variable is a house on a street.
Each house can hold a number of people (value of variable).
But how do you find out how many people (what value is stored)
are in a particular house. You have to have some kind of
address. Then with the address, you can go to the house
and then ask it: How many are there? Then you can get the
value of a variable. This is the basic concept behind pointers.
This should seem very logical, if not please reread this
tutorial and all will make sense.
Let us use this new knowledge to examine a couple of statements.
int var_x;
int* ptrX;
var_x = 6;
ptrX = &var_x;
*ptrX = 12;
printf("value of x : %d", var_x);
The first line causes the compiler to reserve a
space in memory for a integer. The second line
tells the compiler to reserve space to store a pointer.
As you can notice the way you declare a pointer is simply
to add a "*" asterisk to the
end of the data type. A pointer is a storage location for
an address. The third line should remind you of the scanf
statements. The address "&" operator tells
C to go to the place it stored var_x, and then give the
address of the storage location to ptrX.
The fourth line is a bit more complex. An asterisk * in
front of a variable tells C to de reference the pointer,
and go to memory. Then you can make assignments to variable
stored at that location. Since ptrX points to var_x, line
4 is equivalent to this command: var_x = 12; Pretty cool,
eh? You can reference a variable and access its data through
a pointer. Windows and all sorts of programs do this all
the time. They hand pointers of data to each other, and
allow other applications to access the memory they have.
Now you may be wondering, why are pointers so complex,
or I've heard the using pointers can cause problems. It
can, and for those who aren't careful misuse of pointer
can do a lot of damage. Suppose that we forget to type in
line 3 ptrX = &var_x; when we entered the program. What
would happen if we executed it, who knows? Without this
line ptrX is never assigned an address. Basically it points
to some random data anywhere in memory. If you executed
line 4 without line 3. You could get very weird results.
Since ptrX hasn't been pointed to our var_x, maybe its points
to system memory, and then you assign a value someplace
you shouldn't and your computer crashes. This may not always
happen, but it is certainly a possibility, so be very careful
when using pointers. Make sure they are assigned to something
before you use them.
back to top go
to next lesson