The Purpose of Pointers

I've done some scripting before, so I have a basic understanding of programming. C++ is my first "real" language, though. And introduces a lot of new concepts

While many are quite obviously useful, one in particular doesn't strike me as such

Pointers. I understand them, mostly. But I can't think of what use the concept actually is. I don't doubt that it's there for something, but my imagination doesn't seem to stretch to what that is.

Can anyone give some examples of pointers being useful for something, aside from demonstrating how pointers work? Any useful programs which require such a concept?

Actually, pointers (and pointers to pointers!) are one of the most useful tools that C++ has to offer.

Basically a pointer is a variable which holds a memory address rather than a value.

So whereas;

int myVariable;

will set aside a portion of memory to hold an integer value, the following;

int * myPointer;

will set aside a portion of memory which holds the address of _another_ portion of memory which holds an integer value.

Doing this;

*myPointer = myVariable;

means that the pointer now "points" to the memory location holding myVariable.

Now, you'd be right to think that for SIMPLE data structures - integers, chars, and the like - a pointer seems a bit pointless (pardon the pun!) BUT when you start to get to more complex data structures pointers can be VERY useful. In some instances, such as linked lists, pointers are downright indespensable.

It would take a bit of space to explain pointers in depth, so try instead the following two weblinks - both of which give a good introduction.

Taken together these answer the basic "what" and "why" of pointers.

You might also find the wikipedia entry on linked lists helpful - what with linked lists being a very powerful data structure that require pointers in their construction;

Hope it helps.

Last edited on
I've heard one explanation to this question that I felt satisfied with, so here it is, exactly as it was explained to me: In C++ programs, function arguments pass their data "by value" to a local variable inside the called function. This means that the function is not operating on the original value, but a copy of it. Passing a pointer to thte original value instead overcomes this to allow the called function to operate on the original value.

To demonstrate this, the main function in the example below creates a local int variable, named num, together with a pointer to its location. In a call to another function, named triple, the address of the num variable is passed to another pointer. Because the second pointer then also points to the num variable's location, it can be used to assign a new value to the original num variable.

#include <iostream>
using namespace std;
void triple(int * number); //function prototype

int main()
    int num = 5;
    int *ptr = &num; //pointer to num location
    cout<< "num value is "<<num<<endl;
    triple(ptr);      //pass num location to function
    cout<< "num value is now "<<num<<endl;
    int pause;
    return 0;

void triple (int * number)
     *number = *number * 3; //change the value of num

While this may not seem like a big deal since it could be easily duplicated without the use of pointers, (in this case) it at least illustrates the point.
1. Before I am going to explain what is pointer , First I'll try to explain what is main use of the pointers.

"Suppose u know u'r friend name Rengan. He is living Chennai. U have these two details only but u want to meet u'r friend urgently. How will u find u'r friend home without his address(which place,street&door no)".

It is very difficult to find u'r friend home. Then u r going to everyone home and asking whether my friend is there or not?.

How long time will it take compare with suppose u have u'r friend address.
within a few minutes u can find ur friend home with the help of the address.

This same thing is applicable in our program. In our program we may use a lot of variable. If u r going to a use particular variable then cpu will search all the variable from top to bottom.It take a some amount of time.

Suppose if u know the address it is very easy to catch it.

2. pointer is a variable(like int,float,char,...) which holds address of another variables.

int a=10;
int *pa=&a;


At the Compile time, The compiler Checks the Syntax

At the Run time, it execute the first line then it allocate 2 bytes memory of var a with Some address.

In the next line we are getting the address into *pa and finally we r displaying the what is the value in the address.

Pointers. I understand them, mostly.

If you understood them, even mostly, you would not have this question.
Pointers are the very soul of C/C++.
Thank you Gzero. That answers my question perfectly. Pass by value can be very wasteful on memory, so that knowledge will be handy.
Topic archived. No new replies allowed.