Pointers in C++

A pointer is a variable that stores the memory address of another variable. Instead of storing a value like 42 or "hello", a pointer stores where that value lives in the computer's memory. Pointers are one of C++'s most powerful features and are essential for dynamic memory management, arrays, and building complex data structures.

Memory and Addresses

Every variable in a program is stored somewhere in the computer's RAM. Each storage location has a unique address — like a house number on a street. An address in C++ is typically a hexadecimal number like 0x61ff08.

The Address-of Operator &

The & operator gives the memory address of a variable:

#include <iostream>
using namespace std;

int main() {
    int x = 10;
    cout << "Value: "   << x   << endl;
    cout << "Address: " << &x  << endl;
    return 0;
}

Output:

Value:   10
Address: 0x61ff08 (will vary each run)

Declaring a Pointer

data_type *pointer_name;
int *ptr;        // pointer to an integer
double *dptr;    // pointer to a double
char *cptr;      // pointer to a character

Assigning an Address to a Pointer

int x = 42;
int *ptr = &x;    // ptr holds the address of x

cout << "Address in ptr: " << ptr  << endl;
cout << "Value of x:     " << x    << endl;

Dereferencing a Pointer — *

The * operator (dereference) is used to access the value at the address stored in the pointer.

int x = 42;
int *ptr = &x;

cout << "Value via pointer: " << *ptr << endl;

*ptr = 100;   // change x's value through the pointer
cout << "x is now: " << x << endl;

Output:

Value via pointer: 42
x is now: 100

Pointer Example — Full Walkthrough

#include <iostream>
using namespace std;

int main() {
    int num = 50;
    int *ptr = &num;

    cout << "num        = " << num   << endl;
    cout << "&num       = " << &num  << endl;
    cout << "ptr        = " << ptr   << endl;
    cout << "*ptr       = " << *ptr  << endl;

    return 0;
}

Output:

num        = 50
&num       = 0x61ff04
ptr        = 0x61ff04
*ptr       = 50

Null Pointer

A pointer that doesn't point to any valid memory location should be set to nullptr (C++11) or NULL. Dereferencing a null pointer causes a crash — always check before dereferencing.

int *ptr = nullptr;

if (ptr != nullptr) {
    cout << *ptr;
} else {
    cout << "Pointer is null." << endl;
}

Pointers and Arrays

Array names in C++ are essentially pointers to the first element. Pointer arithmetic can be used to traverse arrays:

int arr[] = {10, 20, 30, 40};
int *p = arr;   // p points to arr[0]

for (int i = 0; i < 4; i++) {
    cout << *(p + i) << " ";
}

Output:

10 20 30 40

Dynamic Memory Allocation — new and delete

Using new, memory can be allocated during runtime (on the heap). This memory must be freed using delete when it's no longer needed.

int *ptr = new int;     // allocate memory
*ptr = 75;              // store value
cout << *ptr << endl;   // 75
delete ptr;             // free memory
ptr = nullptr;          // good practice after delete

Dynamic Array:

int n = 3;
int *arr = new int[n];
arr[0] = 5; arr[1] = 10; arr[2] = 15;

for (int i = 0; i < n; i++) {
    cout << arr[i] << " ";
}
cout << endl;

delete[] arr;   // free array memory

Output:

5 10 15

Pointer to Pointer

int x = 10;
int *p = &x;
int **pp = &p;    // pointer to a pointer

cout << **pp << endl;  // Output: 10

Common Pointer Mistakes

MistakeConsequence
Dereferencing a null pointerProgram crash (segmentation fault)
Using delete without newUndefined behavior
Not using delete[] for arraysMemory leak
Dangling pointerPointer pointing to freed memory — dangerous

Key Takeaways

  • A pointer stores the memory address of another variable.
  • & gives the address of a variable; * accesses the value at an address.
  • Always initialize pointers — use nullptr if not immediately assigned.
  • new allocates memory on the heap; delete frees it.
  • Array names are pointers to the first element.

Leave a Comment

Your email address will not be published. Required fields are marked *