C++ Pointers: Memory Addressing, Dereferencing Mechanics & Modern nullptr

A Pointer is a variable that stores the physical hexadecimal memory address of another variable in RAM. Pointers give C++ programmers unparalleled power to directly inspect and manipulate hardware memory with maximum execution speed.

C++ Pointer vs Reference Memory Layout
Figure 5: Memory Mechanics: Direct Variable Aliasing vs Indirect Pointer Addressing

The Two Fundamental Pointer Operators

  1. Address-of Operator (&): Retrieves the physical RAM address of a variable.
  2. Dereference Operator (*): Accesses or modifies the value stored at the address pointed to by the pointer.
#include <iostream>

int main() {
    int target = 42;
    int* ptr = &target; // ptr stores the memory address of target

    std::cout << "Value of target:          " << target << "\n";       // 42
    std::cout << "Memory address (&target): " << &target << "\n";      // e.g., 0x7ffd98
    std::cout << "Address stored in ptr:    " << ptr << "\n";          // 0x7ffd98
    std::cout << "Dereferenced value (*ptr):" << *ptr << "\n";         // 42

    // Modifying the value via pointer dereference:
    *ptr = 100;
    std::cout << "New value of target:      " << target << "\n";       // 100!

    return 0;
}

Pointer Arithmetic

When you increment a pointer (ptr++), C++ does NOT add 1 byte. Instead, it advances the pointer by sizeof(T) bytes, matching the size of the underlying data type:

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

std::cout << *p << "\n";       // 10
p++; // Moves forward by sizeof(int) = 4 bytes!
std::cout << *p << "\n";       // 20
BCA University Exam Alert:

Differentiate between Wild Pointer and Null Pointer:
• Wild Pointer: An uninitialized pointer containing random garbage memory bits. Accessing it crashes the OS.
• Null Pointer: Explicitly points to address zero (nullptr), representing that it intentionally owns no object.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is the difference between nullptr and NULL (or 0) in C++?

In legacy C++, NULL was simply a macro for the integer 0. This caused serious overload ambiguity when a function had both int and pointer overloads (e.g., f(int) and f(char*)). C++11 introduced 'nullptr' of type std::nullptr_t, which is strongly typed and cannot implicitly convert to an integer.

Q2. What is a Dangling Pointer in C++?

A dangling pointer is a pointer that still stores the memory address of an object after that object has been deleted or has gone out of scope. Dereferencing a dangling pointer produces Undefined Behavior (UB). Always set pointers to nullptr after deletion.

Explore Related C++ Tutorials & Next Steps

Boost your C++ proficiency by studying these closely connected core modules:

References vs Pointers

Compare direct aliasing vs indirect addressing.

Pointers and Arrays

How array names decay into pointer offsets.

Smart Pointers

Modern RAII wrappers that eliminate raw pointer leaks.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!