Pointer (C++)

A pointer is a variable that stores the memory address of another value rather than the value itself. You use * to declare a pointer and to read the value it points to, and & to get an address.

C++

Pointers are a core part of C++. The & operator gives the address of a variable, and the * operator (dereferencing) accesses the value stored at that address.

A pointer that points to nothing should be set to nullptr. Misusing pointers is a common source of bugs, which is why modern C++ favors references and smart pointers where possible.

Syntax

type* name = &variable;

Examples

C++

#include <iostream>

int main() {
    int x = 42;
    int* p = &x;
    std::cout << *p << std::endl;
    *p = 7;
    std::cout << x << std::endl;
    return 0;
}

Output:

42
7

Learn it in a lesson

See also

Back to the Dictionary