Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    Memory and pointers · Lesson 11 of 21

    Pointers

    Addresses, * and &, nullptr, and pointers with arrays.

    11 minutes3 games

    Every variable lives somewhere in memory, and that place has an address (a number). A pointer is a variable whose value is another variable's address: it "points" at it.

    • &x means "the address of x".
    • int* p means "p points to an int".
    • *p means "the value p points to" (a dereference).
    C++
    #include <iostream>
    
    int main() {
        int xp = 100;
        int* p = &xp;          // p holds xp's address
    
        std::cout << "xp  = " << xp << "\n";
        std::cout << "*p  = " << *p << "\n";
        std::cout << "same address? " << (p == &xp) << "\n";
    
        *p = 250;              // change xp through the pointer
        std::cout << "xp is now " << xp << "\n";
    
        int* nothing = nullptr;
        if (nothing == nullptr) std::cout << "nothing points nowhere\n";
        return 0;
    }
    xp250int · at 0x7ffd10
    p0x7ffd10int* → xp
    nothingnullptrint* → (nothing)

    p holds xp's address, so *p = 250 changed xp itself. (The real address changes every run.)

    Step by step1 / 6
    1. ▶int a = 5;
    2. 2int b = 9;
    3. 3int* p = &a;
    4. 4*p = *p + 1;
    5. 5p = &b;
    6. 6*p = 0;

    Variables

    a5

    Output

    …

    A pointer can change what it points at.

    Pointers and arrays

    C++
    #include <iostream>
    
    int main() {
        int scores[4] = {90, 75, 88, 60};
        int* p = scores;            // an array name turns into a pointer to its first element
        std::cout << *p << " " << *(p + 2) << " " << p[3] << "\n";
    
        for (int* it = scores; it != scores + 4; ++it) {
            std::cout << *it << " ";
        }
        std::cout << "\n";
        return 0;
    }

    Important

    The three big pointer dangers: 1) dereferencing nullptr (a crash). 2) A dangling pointer to a variable that no longer exists. 3) Walking past an array's end with p + n. All three are undefined behavior. That's why modern C++ minimizes raw pointers in favour of references, containers and smart pointers.

    What's the output?

    #include <iostream>
    int main() {
        int x = 3, y = 7;
        int* p = &x;
        int* q = &y;
        *p = *q;
        p = q;
        *p = 1;
        std::cout << x << " " << y << "\n";
    }

    What will this code print?

    Spot the bug

    This function returns a pointer to something that's gone. Which line is the problem?

    Interview question

    What are dangling and wild pointers, and how do you avoid them?

    Exercise

    Write void swapPtr(int* a, int* b) that swaps the two values through the pointers. The program must print 9 4.

    Expected output: 9 4

    C++
    #include <iostream>
    
    void swapPtr(int* a, int* b) {
        
    }
    
    int main() {
        int x = 4, y = 9;
        swapPtr(&x, &y);
        std::cout << x << " " << y << "\n";
        return 0;
    }

    Learn more from

    • learncpp: Introduction to pointerslearncpp.com
    • learncpp: Null pointerslearncpp.com
    • cppreference: Pointer declarationen.cppreference.com

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    Previousmap, set and unordered_mapNextStack, heap, new and delete
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.