Pointers vs References in C++

Video Tutorial
FREE
Reference Variables thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules
Certificate
Topics Covered

What is Pointer?

A pointer is a variable that stores the memory address of another variable. In other words, it is a variable that tells the computer where to find another variable in memory. Pointers are a powerful feature of C++ that allows you to directly access and manipulate the memory of your program.

The * operator is used to dereference a pointer. For more information, read Pointers in C++

What is Reference?

A reference in C++ is an alias for an already existing variable. This means that a reference shares the same memory address as the variable it references.

References are often used to pass arguments to functions by reference. This allows the function to modify the original variable, rather than creating a copy of it. References can also be used to implement iterators and other data structures.

Here is an example of how to use a reference:

In this example, we are creating a reference to the variable a and then using the reference to increment the value of a.

Differences between Pointers vs References

1. Initialization

In pointers

In reference

Note This differences is based on the Turbo IDE compiler. Other compilers may have different behavior.

2. Reassignment

Pointers can be reassigned to point to different variables. This is useful for implementing data structures such as linked lists and trees. For example:

References, on the other hand, cannot be reassigned once they are initialized. If you try to reassign a reference, you will get a compiler error. For example:

This means that a reference to a reference always refers to the same variable as the original reference.

3. NULL value

Pointers can be assigned a NULL value, which represents an invalid memory address. This can be useful for initializing a pointer before it is used, or for indicating that a pointer does not currently point to any valid object.

References, on the other hand, cannot be assigned a NULL value. This is because a reference is an alias for an existing object, and the object must always have a valid memory address.

This difference can be seen in the following example:

The first line of code is valid, because the * operator indicates that the pointer variable is a pointer. The nullptr keyword is a special value that represents an invalid memory address.

The second line of code is invalid, because the & operator indicates that the reference variable is a reference. However, references cannot be assigned NULL values.

4. Indirection

Pointers enable the creation of double pointers (pointers to pointers), providing additional levels of indirection. On the contrary, references are limited to creating single references (references to variables), offering only one level of indirection.

For instance, with pointers:

In this case, a double pointer q is created, pointing to the pointer p, which in turn points to the variable a. This allows indirect access to the value of a through two levels of indirection.

However, in the context of references:

Creating a double reference q that references the reference p, which in turn references the variable a, is not permissible. References can only be employed to establish single references.

5. Arithmetic operations

Arithmetic operations can be performed on pointers, but not on references. This is because pointers store the memory address of another variable, while references share the memory address of the variable they reference. Arithmetic operations can be used to manipulate the memory address of a pointer, but not the memory address of a reference.

However, it is possible to perform pointer arithmetic on the address of an object pointed to by a reference. This can be done using the & operator to get the address of the object, and then performing arithmetic operations on the address.

Here is an example:

In this example, we are using the & operator to get the address of the object that r references. We then add 5 to the address, and then dereference the address to get the value of the object at the new address.

It is important to note that pointer arithmetic should be used with caution, as it can lead to memory errors if used incorrectly.

References vs Pointers

Certainly! Here's an extended version with 2 additional points for both pointers and references:

PointersReferences
Can be of type voidMust have a data type
Value of a pointer can be changed to another addressCannot be reassigned
Declared using the * operatorDeclared using the & operator
Can have multiple levels of indirection (e.g., double-pointer, triple pointer, etc.)Cannot have multiple indirection levels (except for double references using typedef manipulation)
Can be NULLMust be initialized at the point of declaration; cannot be NULL
Requires dereferencing (*) to access the value at the memory addressAccessed directly without dereferencing (& not needed)
Arithmetic operations can be performed on pointers (e.g., addition, subtraction)Arithmetic operations are not applicable to references

I hope this provides a more comprehensive comparison!

Conclusion

  • References in C++ are an alias for other existing variables.
  • References must be initialized at the time of their declaration and cannot be reassigned.
  • There are different types of references such as lvalue references, rvalue references, and constant references.
  • Function parameters can be passed as references. Functions can return references as well.
  • References are less powerful than pointers but are safer and easier to use.