C++ Course Listing

 

Pointer Operators

by Matthew Martin

 

Contents

Pointers An By Reference

The & Operator

The * Operator

Example Program Illustrating How A Pointer Works

Pointers As Parameters

Pointer Iteration

Example Program Illustrating Pointer Iteration

Pointers And By Reference

A pointer is a special type of variable that contains the memory address of another variable or object. As such, a pointer does not contain what we might normally consider user-related data, but instead the memory address of user-related data. This manner of referring to data is termed, by reference. A pointer is a variable that “points to” the location of data.

top of page

 

The & Operator

The & (ampersand) operator is used to obtain the memory address of a variable or object. The & is placed in front of the variable for which we require the address.

e.g. in order to place the address of the variable x into the variable ptr (which is a pointer):

            ptr = &x;

top of page

The * Operator

The * (star or asterisk) operator is also known as the indirection operator. The indirection arises because we are referring to a value indirectly by reference to its memory location. The * operator is also known as the dereference operator.

The * is placed in front of a variable the value of which we wish to use as a memory address.

e.g. extending the example above for the & operator, if after placing the address of x in ptr (the pointer), we wish to use this later in our program, then we use the * operator. The following line of code will place the value 100 in the memory address pointed to by ptr:

                *ptr = 100;

top of page

Example Program Illustrating How A Pointer Works

// ex_pointer.cpp : Defines the entry point for the console application.

 

// Matthew Martin, 2004

 

#include "stdafx.h"

#include "stdio.h"

 

int main(int argc, char* argv[])

{

                //decalre variables

                int number;

                int *ptr;

 

                //assign a value to number

                number = 10;

 

                //Assign the pointer to point to a memory location using the indirection operator

                ptr = &number;

 

                printf("\nThe value of number: %i", number);

                printf("\nThe value of pointer: %i", ptr); //A memory location

                printf("\nThe value the pointer points to: %i\n", *ptr); //What is at the location pointed to

               

                return 0;

}

top of page

Pointers As Parameters

There are differences between how function parameters work in C and C++. In C all parameters are passed by value. In C++ it is possible to pass parameters by reference. In order to achieve this in C it is necessary to use pointers, explicitly passing by reference.

top of page

Pointer Iteration

Pointers may be iterated, ++, or decremented, --. Incrimination moves the pointer forward in memory by an amount dependant upon the type declared. Thus if the pointer has been declared as an integer (int), then incrimination will advance the pointer by an amount equal to the space allocated in memory for an integer. 

This can best be illustrated with a pointer that points to an element in an array. Remember that each element in an array occupies space in memory of a size dependent upon the type declared for the array and that the memory allocated for the elements of the array is contiguous. The pointer must be of the same type as the array it is assigned to point at. Incrementing the pointer will advance the pointer to the next memory location where the next element in the array begins. This means that is possible to move a pointer along an array in either direction by incrementing and decrementing the pointer.

top of page

Example Program Demonstrating Pointer Iteration

 

// pointer01.cpp : Defines the entry point for the console application.

// A simple program to illustrate using a pointer (ptr) with an array (numbers)

// and the incrimination and decrimintation of the pointer.

 

//Matthew Martin, 2004

#include "stdafx.h"

#include "stdio.h"

int main(int argc, char* argv[])

{

            int i;

            int *ptr;

            int numbers[10] = {1,2,3,4,5,6,7,8,9,10};

            ptr = &numbers[0];

            for(i=0; i<10; i++)

            {

                        printf("\nThe pointer is pointing at the number: %i, in element %i", *ptr, i);

                        if(i<9)   ptr++; //increment the pointer

                        /*we have to stop it incrementing again when we have reached the end

of the array or it will overshoot the end of the array*/

            }

 

            for(i=0; i<10; i++)

            {

                        printf("\nThe pointer is pointing at the number: %i, in element %i", *ptr, i);

                        ptr--; //decrement the pointer

                        /*we have not stopped the pointer undershooting the end of the array.

                        If we were writing more robust code we should prevent this too.

                        */

            }

 

            printf("\n");

            return 0;

} 

by Matthew Martin

top of page