CFORBEGINNERS.COM


C++ Pointers

Pointers often sometimes are hard to understand because it's hard to understand what exactly they are for. When something seems obsolete the less incentive someone has to learn about them. Pointers hold addresses and because of this they can often times be useful tools. In this lesson I wills stick with the pure basics of pointers. Here is the syntax to declare a pointer.

double *p;

Here is pointer named 'p' that can hold the address of floating point values, notice how it required an asterisk in front of the variable to be deemed a pointer. I mentioned that pointers point to addresses here is how to do just that.

p=&x;

'p' now holds the address of the variable 'x' or in other word 'p' now points to 'x', the amersand symbol '&' allows us to do this. Whenver the amersand is put before a variable you are assigning and address and not the actual value. So now that 'p' points to 'x' we can manipulate 'x' by changing 'p' like this.

*p=25.3;

As you have probably already guessed 'x' now equals 23.2, through the power of a pointer. Let's look at a full program to get a better look at pointers.

//Full working program
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
double x;
double *p; //a pointer named 'p'
p=&x; //'p' now points to the address of 'x'
*p=22.4; //initialize the pointer to 22.4

	cout<<x<<endl;

system ("pause");
return 0;
}

This program is very straightforward, a variable is declared and manipulated through a pointers using its address. So right now pointers seem "pointless" (drum sound) but this program is simply to demonstrate how pointers work not necessarily what they are for. Pointers are used to dereference data, this is to manipulate a variable via its address. A good example is functions, in c programming when it comes to functions if you return a variable whose value has changed from within the function it will be returned but with it's original value here is an example of this in a program.

#include <iostream>
#include <stdlib.h>

using namespace std;

int add_5(int n); //Remember you must prototype the function before you use it
int main()
{
    int x;
    cin>>x; //scan in a value for x
    add_5(x);     call the function and send it the value of x
    cout<<x; prints out the value of
system("pause");
return 0;
}

int add_5(int n) n now holds the value of x
{
    n=n+5; n equals what it was plus 5
    return n; return n back to x
}

What this program is supposed to do is take in an integer and then send it to the function add_5, the function then adds 5 to the variable and returns it. However when the variable is returned you will notice that nothing has changed. If the user happened to enter 7 you would expect to recieve 12 as your output but instead you get 7 so what gives. Well in C programming if a variable is changed within a function that change will not carry over back to main. Pointers resolve this problem. Here is the same program that uses pointers instead.

#include 
#include 

using namespace std;

int add_5(int *p);  //prototype your function, this time sending a pointer called *p
int main()
{
    int x;
    cin>>x;
    add_5(&x);  //careful on this one! Call the function add_5 and send it the address of x
    cout< //define the function
{
    *p=*p+5;  //pointer p equals what it was plus 5
    return *p;  //return the pointer p
}

Now if you notice this program runs like a charm all thanks to pointers. The only differnce between this program and the last is that the function accepts a pointer that can be manipulated within a function unlike a normal variable. There are other things that pointers will allow you to do as you will see in future programs but for now we will keep strictly to the basics for the sake of simplicity.