Pointers

Introduction

  • A pointer is a variable that holds the address of another variable.
  • To declare a pointer we use an asterisk.
int *pnPtr; // a pointer to an integer value
double *pdPtr; // a pointer to a double value

Address-of-operator (&)

  • Since pointers only hold addresses, when we assign a value to a pointer, the value has to be an address. To get the address of a variable, we can use the address-of operator (&).
  • The type of the pointer has to match the type of the variable being pointed to.

Dereference Operator (*)

  • A dereferenced pointer evaluates to the contents of the address it is pointing to.
#include <iostream>
 
int main()
{
          using namespace std;
          int nValue = 5;
          int *pnPtr = &nValue; // assign adderss of nValue to pnPtr
          cout << &nValue << endl; // print the address of the variable nValue
          cout << pnPtr << endl; // print the address that pnPtr is holding
          cout << *pnPtr << endl; //print the contents pointed to by pnPtr, which is contents of nValue
}

In the above example:

  • pnPtr is the same as &nValue
  • *pnPtr is the same as nValue

The null pointer

  • Pointers that point to nothing.
  • We assign a pointer a null value by setting it to address 0. int *pnPtr = 0 . This assigns pnPtr address 0.
  • Null pointers are used with dynamic memory allocation.

Use of pointers

  • To dynamically allocate memory in C++.
  • To step through the values in an array.
  • To pass a large struct/class to a function in a way that doesn't involve copying the entire struct/class.
  • To pass a function as a parameter to another function.
  • To achieve polymorphism when dealing with inheritance.

Pointers and Arrays

  • An array is actually a pointer that points to the first element of the array.
int anArray[5] = {9,8,7,6,5};
//dereferencing an array returns the first element (element 0)
cout << *anArray; // points 9
 
char sName[] = "Jason";
cout << *sName; // prints 'J'

Pointer Arithmetic

  • You can perform integer addition or subtraction operations on pointers.
  • When calculating the result of a pointer arithmetic expression, the compiler always multiplies the integer operand by the size of the object being pointed to. This is called scaling.

Pointer arithmetic and arrays

* *(anArray + 1) is the same as anArray[1]

Pointers and const

  • Pointers can be declared constant. A const pointer must be initialized to a value upon declaration, and its value cannot be changed. However, because the value being pointed to is still non-const, it is possible to change the value being pointed to via dereferencing the pointer.
int nValue = 5;
int *const pnPtr = &nValue;
 
cout << *pnPtr << endl;
 
*pnPtr = 6;
cout << *pnPtr << endl;
cout << nValue << endl;
  • A pointer to a constant variable treats the variable as constant when it is accessed through the pointer.
int nValue = 5;
const int *pnPtr = &nValue;
nValue = 6; // OK, nValue is non-const
*pnPtr = 6; NOT OK, pnPtr treats its value as const
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.