Pointer vs Reference in C++: The Final Guide
Tags: cpp, codenewbie, tutorial, beginners
Published: 2021-09-01 06:42:42

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 (not to be confused with a pointer to a constant value!).

## Key differences

### Pointer

- A **pointer** can be initialized to any value anytime after it is declared.
```
int a = 5;
// some code
int *p = &a;
```
- A **pointer** can be assigned to point to a `NULL` value.

- **Pointers** need to be dereferenced with a `*`.

- A **pointer** can be changed to point to any variable of the same type.

Example:
```
int a = 5;
int *p;
p = &a;
int b = 6;
p = &b;
```

### Reference

- A **reference** must be initialized when it is declared.
```
int a = 5;
int &ref = a;
```
- **References** cannot be `NULL`.


- **References** can be used ,simply, by name.

- Once a **reference** is initialized to a variable, it cannot be changed to refer to a variable object.

## Other Differences

### Memory Details

A **pointer** has its own memory address and size on the stack whereas a **reference** shares the same memory address (with the original variable) but also takes up some space on the stack.

###  Arithmetic operations

Various arithmetic operations can be performed on **pointers** whereas there is no such thing called **Reference** Arithmetic.(but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in `&obj + 6`).)

## When use Pointer and when use Reference

The performances are exactly the same, as references are implemented internally as pointers. But still you can keep some points in your mind to decide when to use what :  

Use references :
- In function parameters and return types.
Use pointers: 
- Use pointers if pointer arithmetic or passing NULL-pointer is needed. For example for arrays (Note that array access is implemented using pointer arithmetic).
- To implement data structures like linked list, tree, etc and their algorithms because to point different cell, we have to use the concept of pointers.

As said in a [C++ official FAQ](https://isocpp.org/wiki/faq/references#refs-vs-ptrs) :
> **Use references when you can, and pointers when you have to.**
>
> References are usually preferred over pointers whenever you don’t need “reseating”. This usually means that references are most >useful in a class’s public interface. References typically appear >on the skin of an object, and pointers on the inside.

> The exception to the above is where a function’s parameter or return value needs a “sentinel” reference — a reference that does not refer to an object. This is usually best done by returning/taking a pointer, and giving the nullptr value this special significance (references must always alias objects, not a dereferenced null pointer).

> Note: Old line C programmers sometimes don’t like references since they provide reference semantics that isn’t explicit in the caller’s code. After some C++ experience, however, one quickly realizes this is a form of information hiding, which is an asset rather than a liability. E.g., programmers should write code in the language of the problem rather than the language of the machine.

## Other Resources

- [isocpp.org](https://isocpp.org/wiki/faq/references#refs-vs-ptrs)

______________________________________________________

*For More "The Final Guide" see the [Index Page](https://dev.to/zigrazor/the-final-guide-index-4954)*