top of page

C++ Pointers - Quick Start

Updated: 5 days ago

Today, we explore Pointers - a special type of variable used to store the memory locations of other variables. We will cover what they are, how to use them, and why they can be important.


Quick Start

Here are some useful examples to help you get started using pointers right away!

Getting the memory location address of a variable:


// Displays the address of the variable (Address-of Operator)
cout << "Address of variable: " << &number << endl;

Declaring a pointer:

// An integer variable
int number = 128;
// A pointer "pointing" to an integer variable
int* number_ptr = &number;

Displaying the value at a pointed location:

// Displays the value at the pointed address (Dereference Operator)
cout << "Address of variable: " << *number_ptr << endl;

Full Example:

#include <iostream>
using namespace std;

int main()
{
    // An integer variable.
    int number = 128;
    // A pointer "pointing" to an integer variable.
    int* number_ptr = &number;

    // Displays the value stored in the variable.
    cout << "Value of variable: " << number << endl;
    // Displays the address of the variable (Address-of Operator).
    cout << "Address of variable: " << &number << endl;

    // Displays the value stored in the pointer.
    cout << "Value of pointer: " << number_ptr << endl;
    // Displays the value at the pointed address (Dereference Operator).
    cout << "Value at address: " << *number_ptr << endl;

    // Pointer has the same value as the variables address.
    cout << (&number == number_ptr) << endl; // 1: True, 0: False
    // Variable has the same value as the dereferenced pointer.
    cout << (number == *number_ptr) << endl; // 1: True, 0: False
}

The Results:


The console output after running the example program, demoing the application of pointers in C++.
The console output after running the example program, demoing the application of pointers in C++.


Quick Notes:

  • A pointer is a variable used to store the memory address of another variable.

  • A pointer is declared using an “*” before the pointer's name.

  • A memory location can be retrieved using the address-of operator “&” before the variable's name.

  • A pointer can also be dereferenced using an “*”, revealing the value stored at this location.

  • The address-of operator “&”, used to retrieve a variable's memory address for pointers, is used differently from the reference operator “&”, which uses the same symbol but for a different purpose.



Uses of Pointers:

Pointers allow you to work directly with memory addresses. When passing variables to functions and classes, this is typically done using the pass-by-value method. When this occurs, the function receives a copy of the passed variable, which it stores locally, duplicating the original source. For most use cases, this is acceptable, but it can lead to some risk.


Memory Management

Firstly, when dealing with large amounts of data, passing by value increases the demand on the device's memory. Passing the value by reference, however, by providing the memory address using a pointer, allows you to directly interface with the original variable's memory location rather than creating a local copy. This is very useful when working with microcontrollers that have very restricted memory (e.g. kilobytes over the gigabytes a pc may have available).


Data Consistency

Another benefit of pointers is that directly reading and manipulating data stored in a memory location ensures that all parts of the program use the same information. When programs are spread amongst a vast number of functions, the local copies within are updated separately. This may cause issues where a function uses data that is now out of date. If these functions were utilising the same memory address, then they are using the same version of the data. This allows for a more accurate and less memory-demanding method of sharing data.



Advertisements provided by Google AdSense





For a more in-depth insight into this topic, check out the related posts:


RELATED POSTS

C++ Pointers - An Introduction (Coming Soon)



BIBLIOGRAPHY

[1]

Caleb Curry, C++ Pointers - Finally Understand Pointers, (Oct. 16, 2020). Accessed: Sept. 23, 2025. [Online Video]. Available: https://www.youtube.com/watch?v=rtgwvkaYt1A

[2]

Bro Code, C++ pointers explained easy 👈, (May 20, 2024). Accessed: Sept. 22, 2025. [Online Video]. Available: https://www.youtube.com/watch?v=slzcWKWCMBg

[3]

Bjarne Stroustrup, ‘Pointers, arrays, and References’, in The C++ Programming Language, Forth Edition., Pearson Education, 2018, pp. 171–199.

[4]

Mike McGrath, ‘Pointing to data’, in C++ Programming, 7th Edition., in in easy steps. , pp. 98–114.

[5]

Bro Code, What is a C++ null pointer? ⛔, (May 20, 2024). Accessed: Sept. 23, 2025. [Online Video]. Available: https://www.youtube.com/watch?v=yKT_Rwx4lsQ

[6]

Low Level, you will never ask about pointers again after watching this video, (June 19, 2022). Accessed: Sept. 23, 2025. [Online Video]. Available: https://www.youtube.com/watch?v=2ybLD6_2gKM


Powered and Secured by Wix

bottom of page