Pointers and References

Pointers are a tool available in C++ for giving you more control over a computers memory.  Important uses of pointers include dynamic memory allocation and linked data structures.  They can also be used for manipulating data in arrays and in functions for reference parameters.

Addresses

The address operator & can be used to view the address of a variable.

Example:  Print the address of a variable.

# include<iostream>
using namespace std;

int main( )
{
    int i = 5;
    cout << "address of i " << &i << endl;
    return 0;
}

References

A reference is a synonym or an alias for another variable.  When one variable name is declared to be a reference for another, both variable names refer to the same location in memory.  Changing the value of one variable changes the value of the other variable.  Reference variables can be created using the reference & operator.  Note: (1)  References are pointer constants and (2) The & operator is overloaded to access addresses and create references.

Example: References as aliases.

# include<iostream>
using namespace std;

int main( )
{
    int money = 50;
    int &cash = money;
    cout << "money = " << money << " cash = " << cash << endl;
    money = 100;
    cout << "money = " << money << " cash = " << cash << endl;
    cash = 200;
    cout << "money = " << money << " cash = " << cash << endl;

    return 0;
}

Example: Passing by reference.

# include<iostream>
using namespace std;

void circle (double, double&, double&);

int main( )
{
    double perimeter, area;
    circle(5,perimeter,area);
    cout << "perimeter = " << perimeter << endl;
    cout << "area = " << area << endl;
    return 0;
}

void circle (double radius, double &perimeter, double &area)
{
    const double pi = 3.14259;
    perimeter = 2*pi*radius;
    area = pi*radius*radius;
}

Pointers

Declaring pointers

A pointer is a variable that stores a memory address.  A pointer may be declared using the * operator.

Initializing pointers

All pointers should be initialized to the address of another variable.  If the address of another variable is not available yet, set the pointer to null.

Dereferencing pointers

The dereference operator * can be used to obtain the value to which the pointer points.  Note the * operator has been overloaded to (i) multiply several different data types (ii) declare a pointer and (iii) dereference a pointer.

Example:  Declare and initialize a pointer variable.  Point to different places in memory.  Dereference a pointer.

# include<iostream>
using namespace std;

int main( )
{
    int myAge = 35;
    int yourAge = 20;
    int *pAge = &myAge;

    cout << "address of myAge " << pAge << endl;
    cout << "*pAge = " << *pAge << endl;
    pAge = &yourAge;
    cout << "address of yourAge " << pAge << endl;
    cout << "*pAge = " << *pAge << endl;

    return 0;

Pointers and arrays

An array name is a constant pointer to the first element of an array.  In the example below, data is a pointer to &data[0].

Example: Using pointers to traverse an array.

# include<iostream>
using namespace std;

int main( )
{
    int *ptr;
    int data[10] = {100, 90, 80, 70, 60, 50, 40, 30, 20, 10};
    for (ptr = data; ptr < data + 10; ptr++)
       cout << *ptr << endl;
    return 0;
}

Example:  Dynamic memory allocation.

# include<iostream>
using namespace std;

int main( )
{
    int i, size;
    cout << “enter size of the array \n”;
    cin >> size;
 
    int * list = new int[size];

    for (i = 0, i < size; i++)
    {
        list[i] = i;
        cout << list[i];
    }
 
    delete [ ] list;
    return 0;
}

Summary

  1. A pointer is a variable that holds the address of another variable.
  2. A reference is just another name for the variable assigned to it and can be used just like the original variable.
  3. The * operator is overloaded to
    (i) declare pointer variables and
    (ii) dereference pointer variables
  4. The & operator is overloaded to
    (i) determine variable addresses and
    (ii) declare references.
  5. Pointers can be reassigned.  References cannot be reassigned.

Resources