Sahaj.blogs
14 Nov 2023 · StudyPhase 2 · C++

C++ · Pointers

What a pointer is, & and * operators, nullptr, pointer arithmetic over an array, and why dangling pointers are dangerous.

This is the topic Python hid from me. A pointer is a variable whose value is a memory address.

  • &x gives the address of x.
  • int* p = &x; declares a pointer to an int.
  • *p dereferences the pointer: it reads or writes the value at that address.
  • nullptr means “points to nothing”. Dereferencing it is undefined behaviour.
  • Pointer arithmetic: p + 1 moves forward by one element, not one byte.

Python names behave like references to objects. C++ lets me work with the addresses directly, which is powerful but easy to get wrong.

#include <iostream>

void bump(int* p) { if (p) *p += 10; }   // modify caller's variable

int main() {
    int x = 5;
    int* p = &x;
    std::cout << "x=" << x << " *p=" << *p << " (p holds an address: " << (p != nullptr) << ")\n";
    *p = 42;
    std::cout << "after *p = 42, x=" << x << '\n';

    bump(&x);
    std::cout << "after bump, x=" << x << '\n';

    int arr[4] = {10, 20, 30, 40};
    int* q = arr;                          // array decays to pointer to first element
    for (int i = 0; i < 4; ++i) std::cout << *(q + i) << ' ';
    std::cout << "\ndistance arr[3]-arr[0]: " << (&arr[3] - &arr[0]) << " elements\n";

    int* none = nullptr;
    bump(none);                            // safe: checked inside
    std::cout << "null handled\n";
}

Output:

x=5 *p=5 (p holds an address: 1)
after *p = 42, x=42
after bump, x=52
10 20 30 40 
distance arr[3]-arr[0]: 3 elements
null handled

#cpp#pointers#memory

Sources

  1. Pointer declaration — cppreference.com
  2. Introduction to pointers — LearnCpp.com
  3. nullptr — cppreference.com