A pointer in C++ is a variable that holds the memory address of another variable. A reference is an alias for an already existing variable. Once a reference is initialized to a variable, it cannot be changed to refer to another variable. Hence, a reference is similar to a const pointer.

Below example shows the use of pointer and reference.

// Assignment using pointer
int i;
int *pi = &i
*pi = 4;

// Assignment using reference
int i;
int &ri = i;
ri = 4;

Pointer variable pi stores the address of variable i i.e. it points to the memory location of i. Creating a reference to i, makes a alias for it. ri does not point to i by storing its memory address in separate memory location.

Pointer vs Reference
Pointer vs Reference

Pointer Vs Reference

Key difference between pointer and reference are

  • A pointer can be initialized to any value anytime after it is declared. A reference must be initialized when it is declared.
  • A pointer can be assigned to point to a NULL value. References cannot be NULL.
  • You can have pointers to pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection.
  • A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly.
  • Address operator will return the address of pointer variable. It’s not possible to get the address of a reference. Address operator will return the address of the referenced variable instead
  • Pointer supports arithmetics , but it is not possible to do arithmetics on references.