Pointers
A pointer is a special type of variable that stores the memory address of another variable.
Creating Pointers
A pointer variable is declared using the indirection operator (*
). This is a special unary operator that signals the program to store the memory address of another variable.
To get the memory address of a variable to store in the pointer variable, the address operator (&
) is used. This signals the program to return the memory address of a variable. Memory addresses are usually prefixed with 0x
; this indicates that the following digits are in hexadecimal notation.
std::string game = "Cricket";std::cout << game << "\n"; // Output: Cricketstd::cout << &game << "\n"; // Output of game memory address: 0x6dfed4// A pointer with the name ptr, that stores the address of gamestd::string* ptr = &game;std::cout << ptr << "\n"; // Output of pointer to game: 0x6dfed4
Pointer Dereferencing
Dereferencing a pointer means getting the value that a pointer variable is pointing at.
Only a limited number of tasks can be accomplished solely using the memory address stored in pointers. To perform various operations, it’s essential to access the actual value stored at the memory address, allowing for direct manipulation.
This can be done by dereferencing a pointer. The indirection operator (*
) used for declaring a pointer can be used to dereference a pointer as well.
When a pointer is dereferenced, it tells the program to return the value contained in the specified memory address and then operations can be performed on it.
std::string game = "Cricket"; // Variable declarationstd::string* ptr = &game; // Pointer declarationstd::cout << *ptr << "\n"; // Output of value dereferenced from ptr: Cricket*ptr = "Baseball";std::cout << *ptr << "\n"; // Output of value dereferenced from ptr: Baseball
Null Pointers
Similar to other data types, pointers can be declared without being assigned a value.
int *ptr;
This however is a cause of concern because if the program tries to dereference and manipulate a pointer that holds no memory address, it could lead to some unexpected behavior. To prevent this, nullptr
is used. It is a special kind of value that prevents pointers from behaving unexpectedly when they hold no memory address.
int *ptr = nullptr; // Assigns the pointer to a null pointer valuestd::cout << *ptr << "\n"; // 0std::string game = "Cricket";*ptr = &game;std::cout << *ptr << "\n"; // Cricket
Pointers As Function Parameters
Pointers can also be passed into functions as arguments. This helps to manipulate values from within a function even though the value lives outside that function.
void changeGame(std::string *game, std::string new_game) { // defining a pointer as a function parameter*game = new_game; // Dereference the pointer and change the game}std::string game = "Cricket";changeGame(&game, "Baseball"); // Passing a pointer as a function argumentstd::cout << game << "\n" // Baseball
Pointers
- nullptr
- Represents a null pointer in C++, ensuring type safety and indicating that a pointer does not point to any valid memory location.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours