← Back to list

cpp_035: Pointers and References in C++

Introduction

CodeAddict · 2025-06-06 01:01 · 0 claps · 5.6 min read
#pointers #pointers-in-cpp #reference #pointer-to-pointer #nullpointer
Open on Medium ↗

cpp_035: Pointers and References in C++

Introduction

I still remember when I first started learning C++ years ago — I was confused by pointers and references, especially because of the symbols * and &. I kept making mistakes until I finally understood how they really worked and how to remember them properly. So I thought it would be helpful to put together a simple, clear guide for anyone else who might be struggling with these concepts too.

In C++, pointers and references are mechanisms to work with memory addresses and create aliases for variables. They are fundamental for tasks like dynamic memory management, passing data to functions, and building complex data structures.

Quick Summary: Pointers vs References

Before we dive into details, here’s a quick comparison to set the stage:

  • Pointers are variables that store memory addresses. ➤ You use * to dereference (get the value), and & to get the address of something. ➤ You can reassign a pointer to point to different variables, or even nullptr. (In C++, nullptr is a special keyword that represents a null pointer — a pointer that doesn’t point to any valid memory location.)
  • References are aliases for existing variables. ➤ You use & in the declaration to create a reference. ➤ Once bound to a variable, a reference cannot be changed to refer to another.

1. Declaring and Dereferencing a Pointer

Syntax:

type* pointer_name = &variable;
int* ptr = &x;

Explanation: A pointer is a variable that holds the memory address of another variable. The & operator retrieves the address of a variable, and the * operator accesses the value at that address, so what we did above:

  • Declares a pointer to store the address of a variable.
  • *pointer_name — Dereferences the pointer to access the value at the stored address.

Example:

#include <iostream>
using namespace std;

int main() {
    int x = 10;
    int* ptr = &x;

    cout << "Value of x: " << x << endl;
    cout << "Address of x: " << &x << endl;
    cout << "Pointer ptr stores: " << ptr << endl;
    cout << "Value at ptr: " << *ptr << endl;

    return 0;
}

Output:

(Note: The memory address shown below is from my computer. When you run the program, you’ll see a different address based on your system.)

Value of x: 10
Address of x: 0x553c5ffc14
Pointer ptr stores: 0x553c5ffc14
Value at ptr: 10

Explanation:

  • int* ptr = &x; declares a pointer ptr that stores the address of x.
  • &x gives the memory address of x.
  • *ptr dereferences ptr to access the value of x (10).
  • The output shows the value of x, its address, the address stored in ptr, and the value accessed via ptr.

2. Modifying a Value via a Pointer

Syntax:

*pointer_name = value;

Explanation: By dereferencing a pointer, you can directly modify the value of the variable it points to, affecting the original variable.

Example:

#include <iostream>
using namespace std;

int main() {
    int x = 20;
    int* ptr = &x;

    cout << "Before modification: x = " << x << endl;
    *ptr = 30;
    cout << "After modification: x = " << x << endl;

    return 0;
}

Output:

Before modification: x = 20
After modification: x = 30

Explanation:

  • ptr points to the address of x.
  • *ptr = 30; changes the value at the address ptr points to, updating x to 30.
  • The output confirms that modifying *ptr changes the value of x.

3. Modifying a Pointer’s Address

Syntax:

pointer_name = &another_variable;
  • Changes the address stored in the pointer to point to a different variable.

Explanation: A pointer can be reassigned to point to a different variable’s address, allowing it to reference a new variable without altering the original variable’s value.

Example:

#include <iostream>
using namespace std;

int main() {
    int x = 40;
    int y = 50;
    int* ptr = &x;

    cout << "Before: ptr points to x = " << *ptr << endl;
    ptr = &y;
    cout << "After: ptr points to y = " << *ptr << endl;

    return 0;
}

Output:

Before: ptr points to x = 40
After: ptr points to y = 50

Explanation:

  • Initially, ptr points to x (value 40).
  • ptr = &y; reassigns ptr to point to y (value 50).
  • The output shows that ptr now accesses y’s value, while x remains unchanged.

4. Pointer to Pointer

Syntax:

type** pointer_to_pointer = &pointer;
  • type** pointer_to_pointer = &pointer; Declares a pointer to a pointer.
  • **pointer_to_pointer — Dereferences twice to access the original value.

Explanation: A pointer to a pointer stores the address of another pointer. This is useful for modifying pointers in functions or managing dynamic multi-dimensional arrays.

Example:

#include <iostream>
using namespace std;

int main() {
    int x = 60;
    int* ptr = &x;
    int** ptr_to_ptr = &ptr;

    cout << "Value of x: " << x << endl;
    cout << "Value via ptr: " << *ptr << endl;
    cout << "Value via ptr_to_ptr: " << **ptr_to_ptr << endl;

    return 0;
}

Output:

Value of x: 60
Value via ptr: 60
Value via ptr_to_ptr: 60

Explanation:

  • ptr points to x.
  • ptr_to_ptr points to ptr.
  • **ptr_to_ptr dereferences twice: first to get ptr, then to get x’s value (60).
  • The output shows that all methods access the same value.

5. Declaring a Reference

Syntax:

type& reference_name = variable;
  • type& reference_name = variable; =>Declares a reference as an alias for a variable.

Explanation:

A reference is an alias for an existing variable. Unlike pointers, references cannot be reassigned and do not require dereferencing to access the value.

#include <iostream>
using namespace std;

int main() {
    int x = 70;
    int& ref = x;

    cout << "Value of x: " << x << endl;
    cout << "Value via ref: " << ref << endl;
    cout << "Address of x: " << &x << endl;
    cout << "Address of ref: " << &ref << endl;

    return 0;
}

Output:

(Note: The memory address shown below is from my computer. When you run the program, you’ll see a different address based on your system.)

Value of x: 70
Value via ref: 70
Address of x: 0x80889ffce4
Address of ref: 0x80889ffce4

Explanation:

  • int& ref = x; makes ref an alias for x.
  • Accessing ref is equivalent to accessing x.
  • &x and &ref show the same address, confirming that ref is not a separate variable but an alias.

6. Modifying a Value via a Reference

Since a reference is an alias, assigning a value to the reference directly modifies the original variable.

#include <iostream>
using namespace std;

int main() {
    int x = 80;
    int& ref = x;

    cout << "Before: x = " << x << endl;
    ref = 90;
    cout << "After: x = " << x << endl;

    return 0;
}

Output:

Before: x = 80
After: x = 90

Explanation:

  • ref is an alias for x.
  • ref = 90; changes the value of x to 90.
  • The output shows that modifying ref updates x.

7. Pointer as a Function Parameter

Passing a pointer to a function allows the function to modify the original variable (pass-by-reference).

Example:

#include <iostream>
using namespace std;

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 100, y = 200;

    cout << "Before swap: x = " << x << ", y = " << y << endl;
    swap(&x, &y);
    cout << "After swap: x = " << x << ", y = " << y << endl;

    return 0;
}

Output:

Before swap: x = 100, y = 200
After swap: x = 200, y = 100

Explanation:

  • swap(&x, &y) passes the addresses of x and y to the function.
  • Inside swap, a and b dereference the pointers to swap the values of x and y.
  • The output shows the values of x and y are swapped.

8. Reference as a Function Parameter

References provide a cleaner way to achieve pass-by-reference compared to pointers, as no dereferencing is needed.

#include <iostream>
using namespace std;

void swap_ref(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 300, y = 400;

    cout << "Before swap: x = " << x << ", y = " << y << endl;
    swap_ref(x, y);
    cout << "After swap: x = " << x << ", y = " << y << endl;

    return 0;
}

Output:

Before swap: x = 300, y = 400
After swap: x = 400, y = 300

Explanation:

  • swap_ref(x, y) passes x and y by reference.
  • Inside swap_ref, a and b are aliases for x and y, so modifying them swaps the values of x and y.
  • The output confirms the swap.

Key Takeaways

  • Pointers: Use for dynamic memory, arrays, or pass-by-reference in functions. Be cautious with null pointers.
  • References: Simpler alternative to pointers for aliasing and pass-by-reference, with no need for dereferencing.

메타데이터
post_id
eee16f4c5f52
slug
cpp-035-pointers-and-references-in-c-eee16f4c5f52
url
https://medium.com/@staytechrich/cpp-035-pointers-and-references-in-c-eee16f4c5f52
canonical_url
https://medium.com/@staytechrich/cpp-035-pointers-and-references-in-c-eee16f4c5f52
author_url
https://medium.com/@staytechrich
status
ok
fetched_at
2026-07-19 01:01:37