What is a Pointer?
Part of the Logic & Flow section of Coddy's C++ journey — lesson 1 of 56.
A pointer is a special type of variable that stores the memory address of another variable, rather than storing a value directly. Think of it like a street address that tells you where to find a house, instead of being the house itself.
When you create a regular variable like int x = 42;, the computer stores the value 42 in a specific location in memory. A pointer to this variable would store the address of that memory location, allowing you to indirectly access and manipulate the original variable.
To declare a pointer in C++, you use the asterisk (*) symbol in the declaration:
int* ptr; // Declares a pointer to an integer
double* dPtr; // Declares a pointer to a doublePointers are fundamental to many advanced C++ features because they enable direct memory manipulation. This makes them powerful tools for creating efficient programs, managing dynamic memory, and building complex data structures.
Understanding pointers opens the door to working with arrays, functions, and object-oriented programming concepts more effectively.
Cheat sheet
A pointer stores the memory address of another variable, rather than storing a value directly.
To declare a pointer, use the asterisk (*) symbol:
int* ptr; // Pointer to an integer
double* dPtr; // Pointer to a doublePointers enable direct memory manipulation and are fundamental for advanced C++ features like dynamic memory management and complex data structures.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers and Memory
What is a Pointer?Address-Of OperatorDereference OperatorNull PointersPointers and ArraysDynamic Memory with 'new'Freeing Memory with 'delete'Recap - Pointer Practice2Vectors (Dynamic Arrays)
Introducing std::vectorCreating a VectorAdding ElementsAccessing ElementsVector SizeIterating with a For LoopRange-Based For LoopRemoving ElementsRecap - Vector Operations5Project: Inventory Tool
Project SetupAdding and Updating Items