1. Intro
A pointer is a variable that stores the memory address of another variable, instead of storing a data value directly. Every variable in a running program lives at some address in memory, and a pointer lets you capture that address and work with it. Pointers are central to C++ because they enable dynamic memory management, efficient array/function handling, and low-level control that is not possible with plain variables alone.
Cricket analogy: A pointer is like a locker number written on a card rather than the bat itself — the card doesn't hold the bat, it tells you exactly which locker in the pavilion, like Room 12, currently holds Virat Kohli's bat.
2. Syntax
dataType* pointerName; // declaration
pointerName = &variable; // assignment (address-of)
*pointerName; // dereference (access the value)3. Explanation
A pointer declaration such as int* p; creates a variable p that is meant to hold the address of an int. The & operator, called the address-of operator, returns the memory address of a variable — &x gives the address where x is stored. The * operator, called the dereference operator, is used in two different contexts: in a declaration (int* p) it marks p as a pointer type, while in an expression (*p) it means "go to the address stored in p and get the value there". Every pointer also has a type (int*, double*, char*, ...) which tells the compiler how to interpret the bytes at that address and how far to move for pointer arithmetic.
Cricket analogy: Declaring int* p is preparing an empty locker tag meant for a batting-average box; &average gives you the actual locker number where Kohli's average lives, and *p means "open that locker and read the number inside" — the tag's type tells you it's sized for an average, not a full scorecard.
4. Example
#include <iostream>
using namespace std;
int main() {
int x = 42;
int* p = &x; // p stores the address of x
cout << "Value of x: " << x << endl;
cout << "Address of x: " << &x << endl;
cout << "Value stored in p: " << p << endl;
cout << "Value pointed to by p: " << *p << endl;
*p = 100; // modify x through the pointer
cout << "New value of x: " << x << endl;
return 0;
}5. Output
Value of x: 42
Address of x: 0x7ffee4c5a9ac
Value stored in p: 0x7ffee4c5a9ac
Value pointed to by p: 42
New value of x: 1006. Key Takeaways
- A pointer is a variable that stores a memory address, declared as
dataType* name;. &variableyields the address of that variable (address-of operator).*pointerdereferences the pointer, giving access to the value at that address.- Modifying
*pchanges the original variable, sinceppoints directly to its memory location.
Practice what you learned
1. What does the `&` operator do when applied to a variable?
2. What is the correct way to declare a pointer to an `int`?
3. If `int* p = &x;`, what does `*p` represent?
4. What happens after `*p = 100;` if `p = &x;`?
5. Why does every pointer need a type, such as `int*` or `double*`?
Was this page helpful?
You May Also Like
Pointer Arithmetic in C++
Understand how adding or subtracting integers from a pointer moves it by whole elements, scaled by the size of the pointee type.
Pointers and Arrays in C++
See how an array name decays into a pointer to its first element, and how `arr[i]` and `*(arr + i)` are two ways of writing the same thing.
Null Pointers in C++
Learn what a null pointer is, why modern C++ prefers `nullptr` over the old `NULL` macro, and why dereferencing one causes undefined behavior.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics