C++ Pointers and References | Pointer Basics, Arithmetic, nullptr and References


This complete tutorial on C++ Pointers and References explains how memory addresses are handled in C++. It covers pointer basics, pointer arithmetic, using pointers with arrays and functions, nullptr, references, and common pointer-related issues such as dangling and wild pointers. The tutorial follows best coding practices and helps beginners understand memory management concepts essential for efficient and safe C++ programming.

Pointers and References – Complete Tutorial

1. Pointer Basics

A pointer is a variable that stores the memory address of another variable.

Syntax:


data_type *pointer_name;

Example:


int x = 10;
int *ptr = &x;

cout << ptr; // Address of x
cout << *ptr; // Value of x

Key Points:

  1. & gives the address of a variable
  2. * is used to declare and dereference a pointer

2. Pointer Arithmetic

Pointer arithmetic allows pointers to move through memory locations.

Example:


int arr[3] = {10, 20, 30};
int *ptr = arr;

cout << *ptr; // 10
cout << *(ptr + 1); // 20

Operations Allowed:

  1. Increment (ptr++)
  2. Decrement (ptr--)
  3. Addition and subtraction

Best Practices:

  1. Perform pointer arithmetic only within valid memory ranges
  2. Avoid accessing memory outside array bounds

3. Pointers with Arrays

Arrays and pointers are closely related in C++.

Example:


int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;

for (int i = 0; i < 5; i++) {
cout << *(ptr + i) << " ";
}

Key Concept:

  1. Array name acts as a pointer to the first element

4. Pointers with Functions

Pointers can be passed to functions to modify original data.

Example:


void update(int *p) {
*p = 20;
}

int main() {
int x = 10;
update(&x);
cout << x; // Output: 20
}

Advantages:

  1. Efficient memory usage
  2. Ability to modify actual variables

5. nullptr

nullptr is a keyword representing a null pointer in modern C++.

Example:


int *ptr = nullptr;

Why use nullptr?

  1. Avoids ambiguity with integer 0
  2. Improves type safety
  3. Preferred over NULL

6. References

A reference is an alias for an existing variable.

Syntax:


data_type &reference_name = variable;

Example:


int x = 10;
int &ref = x;

ref = 20;
cout << x; // Output: 20

Key Differences from Pointers:

  1. Must be initialized
  2. Cannot be null
  3. Cannot be reassigned

7. Dangling and Wild Pointers

Dangling Pointer

A pointer that points to a memory location that has been freed.

Example:


int *ptr = new int(10);
delete ptr;
// ptr is now dangling

Wild Pointer

A pointer that is not initialized.

Example:


int *ptr; // Wild pointer

Best Practices to Avoid Issues:

  1. Initialize pointers
  2. Set pointers to nullptr after deletion
  3. Avoid returning addresses of local variables

Common Mistakes to Avoid

  1. Dereferencing uninitialized pointers
  2. Forgetting to free dynamically allocated memory
  3. Confusing pointers with references
  4. Using deleted pointers

Best Practices for Pointers and References

  1. Prefer references when possible
  2. Use smart pointers in modern C++
  3. Minimize raw pointer usage
  4. Always initialize pointers

Summary

In this chapter, you learned about C++ pointers and references, including pointer basics, arithmetic, arrays, functions, nullptr, references, and common pointer issues. Mastering these concepts is crucial for understanding memory management and advanced C++ programming.