Overview

Why use pointers?

What's a pointer?

Declaring pointers

Initializing/Setting pointers

Pointer operators

Pointer arithmetic

Pointers as parameters

Pointers vs. arrays

Array of pointers

Function pointers

 


About pointers

  • Normally a variable directly contains a specific value
  • A pointer contains an address of a variable that contains a specific value
  • Used to indirectly access data by address instead of by name
  • One pointer can point to only one data type

 

Example:

Below is shown the representation of variables in memory. For each declared variable, C++ compiler allocates storage in the memory and associates it with the variable. To access the variable's value, c++ compiler uses the address, not the variable name. So, the variable y holds value of '32'; variable x holds value of 'A'; and xPtr is a pointer variable and instead of holding a value it holds an address to another variable, in this case address of variable 'x'.