Why Use Pointers?

» 22 November 2010 » In C/C++, Programming »

This is a very much asked questions, especially among students. Pointers, at first, is very hard to understand. All those asterisks * and ampersands & and the crazy assignments. Well, I assume you already understand pointers, or rather, you have a an idea how generally pointers operate. Then you might wanna ask, why use pointers?

Pointers Reviewed

As we all (should) know, pointers stores the address of a variable. So basically, when we call a pointer by name, we’re referring to the address. Let’s take this for example:

int *p1

The above declarations tell the compiler that p1 is a pointer to an integer. Means that you cannot assign an address of a character or a floating point to it. Actually you might want to try, but basically you’re just wasting time. Anyway, let’s say we have this variable a with the value of 5. And then we do this:

p1 = &a

We just assigned the address of a to the pointer p1. So if we are to print p1 it’s just like, we’re printing the value of &a which is the address of a. So how do we know that its the address of a? Answer: the ampersand &.

Accessing Values with Pointers

So on the above review example, we assigned &a to p1. But what if we are to access the value of a using pointers? Normally we access some variables value by its name. But now, since we are using pointers, we are going to access the value by the variable’s pointer. Say for example, we are to print the value of a. Normally we will do:

std::cout << a;

But if we are to use pointers, we do this:

std::cout << *p;

Remember, we assigned the address of a to p1, and if we are to access the value of a with a pointer, we use the asterisk * symbol, like *p1.

Now, to make it less confusing, let’s say we do not have any pointers. And we have &a as the address of a. To access its value, we have to use the asterisk symbol, and it should be like *&a. Still confused? I guess not.&a here is our reference to the value of a, and by using an asterisk, we dereferenced it. Making an asterisk * a Dereference Operator.

Using Pointers in Functions

Whenever we pass variables to a function, let’s say main called the function nUpdate(); which accepts 2 parameters, one is a variable of type int to be updated, and the other one is the constant integer to be added to the variable. Normally the function cannot directly alter the variable that will be passed to it, instead it will copy the variable and create another one with the same attributes and just return it to the main function. Here’s how it will look like:

int nUpdate(int a, const int b) {
    return a + b;
}

int main() {
    int a = 5;
    a = nUpdate(a, 6);
    return 0;
}

After main called nUpdate() the value of a now is 11. Am I right? Now, this is correct. But, there’s another way. Without wasting some bits, we can just directly update a in the function. How? Pointers. Now, this is one major use of pointers, and I’ll explain it further later. Using pointers, we are going to pass just the address of our variable a and the constant. So we have to make changes, on the parameter type of the function, and we need something that holds addresses. Ah, pointers.

int nUpdate(int *p, const int b) {
    *p += b;
}

int main() {
    int a = 5;
    nUpdate(&a, 6);
    return 0;
}

There we have it. We passed the address of a which is &a, and the pointer called p receives the address, and we accessed it by using our dereference operator * just like what we did earlier. Now there’s another way of doing the very same thing, except we will not be directly using a pointer. If you can remember earlier, and using our latest example, pointer p holds the address of a which is &a. So, we can do this:

int nUpdate(int &a, const int b) {
    a += b;
}

int main() {
    int a = 5;
    nUpdate(a, 6);
    std::cout << a;
    return 0;
}

This approach, is what I use. As you can see, our function now accepts an address of an integer variable. And in our main, we passed the variable a. But from the function’s point of view, instead of receiving the value of a, it grabs its address. And to access the value of that address we again should use the dereference operator *, and *&a is equal to a.

A Larger View

The past examples showed how useful pointers are in functions. But to show you more of its importance, look at a bigger view. Say there’s this large program, its main calls some function and passes loads of data, say millions of bytes (MB). Without the use of pointers, this is heavy. As the function will copy all those data to its scope in memory, and do its job. It will increase memory usage to be short. Now with the help of pointers, the function could just get the address of all those variables which holds the data and do its job. No copying, less memory usage.

Moving On

I surely hope that this is very clear for you now, if you are still confused, then C/C++ is not your type. Anyway, learning takes time and good luck with pointers!

Tags: , , , ,

Trackback URL

  • Brent

    Wow, I never seen a pointer tutorial so thorough, thanks for this and yeah, one of my life’s questions is answered by this. :D

  • Pingback: PHP Tutorial #1 – Covering the basics | PHP5 Web Hosting

  • Anonymous

    Nice article. I found your blog because I’m working on a python script that accesses data from facebook. This article intrigued me because I have been teaching myself C over the past few months (I’m a highschool student, entirely self-taught). This article is good for teaching pointers, but I don’t like how it ends by saying “if you are still confused, then C/C++ is not your type.” Pointers are a hard topic, and a student could easily not understand them even after reading this article, possibly because of a lack of knowledge in other areas (e.g. memory management). Ending your article on such a note could be extremely discouraging to an aspiring computer scientist who is having trouble taking his/her first steps in low level programming.

  • Jens

    When I started read this, i was wondering why use pointers when you got (type& var) in function arguments, not really answered :/