You are on page 1of 4

Pointers

"Do not mistake the pointing finger for the moon." -ZEN SAYING
Pointers are one of the most powerful features of C++, and the book has very nice text on them.
The basic vibe is this: A variable such as an int consumes storage. You access that storage by using
the variable name. A pointer is a variable that points to the storage of a variable. Sometimes we
call this the "address" of a variable. To access the storage, you need to "dereference" the pointer.
pex1.cpp
You'll note, you access the address of i with &i, and you dereference the pointer i_ptr with *i_ptr.
Here is the output of this when run on my office desktop (different machines will print different
pointer values)
The value of i_ptr is not overly important, except that it is the same as the address of i. Since both
i and *i_ptr access the same storage, changing one of them changes the other. This is reflected in
the last four lines of the program -- as you can see, when we change *i_ptr, i is changed as well.

Pointers in Functions
When you pass a pointer as a parameter to a function, then when you dereference it in the
function, you will modify whatever it points to. This causes a side effect. pex2.cpp, the procedure
a() modifies i in main() because it has been passed a pointer to i's storage
One of the biggest uses of pointers is to in effect return two things from a function. For example,
the function below determines both the square and the cube of a number: pex3.cpp,

3. Pass By Value -- Always!! (with one small exception)


I can't stress this enough -- regardless of what the book says, C++ (usually) and C (always) pass
arguments by value. That means that they copy their arguments when they pass them to
procedures. Which means that when the procedure changes the arguments' values, it does not
affect the original copies. Note: reference variables in C++ are the exception.
For example, even though the procedure a( ) changes i, that does not modify the variable i in
main(). This is because i has been "passed by value." pex4.cpp
Now, the same thing is true when you pass a pointer as an argument to a procedure -- a copy is
made of the pointer. However, when you deference the copy, that will change whatever the
pointer points to. For example, look at the following program: (In pex5.cpp).

4. Const
The const qualifier tells the compiler that the value of a variable should not be modified. Use
constants to protect your values whenever possible.

Example: const int COUNT = 101; // COUNT cannot be modified


const char name[COUNT] = "Lebron James"; // elements of name cannot be
modified

5. The array/pointer duality


An array variable is a pointer to the first element of an array. For that reason, you may set a
pointer equal to an array, and that has the pointer point to the first element of the array. Here's
pex6.cpp:
In fact, you can use standard array accessing syntax (square brackets) on the pointer, just like you
do with an array. You can see this in pex7.cpp:

The reason why this is important is that when you pass an array to a function as an argument, you
are passing a pointer to its first element. This means that if you change the contents of the array
inside the function, that will change the contents of the original array. This is because only the
pointer to the contents is copied in the function call. It is important to understand this.
So look at pex8.cpp:

You might also like