Pointers in C++
In this lesson: Learn C++ pointers — &, *, nullptr, pointer arithmetic and pointers with arrays — diagrams and output. In Hindi & English.
What is a pointer?
Every variable lives at some address in memory. A pointer is a variable that stores such an address instead of a normal value. In other words, a pointer "points to" where another variable lives.
Declaring a pointer
Put a * between the type and the name. The type says what kind of variable it points to:
int x = 42; int *p = &x; // p holds the address of x
Address-of and dereference
| Operator | Meaning |
|---|---|
&x | Address-of: the memory address of x |
*p | Dereference: the value stored at the address in p |
So p = &x makes p point to x, and *p reads or changes x through that pointer.
A working example
#include <iostream>
using namespace std;
int main() {
int x = 42;
int *p = &x;
cout << "x = " << x << "\n";
cout << "*p = " << *p << "\n"; // value via pointer
*p = 100; // change x through p
cout << "x now = " << x << "\n";
return 0;
}x = 42
*p = 42
x now = 100
nullptr
A pointer that points to nothing should be set to nullptr, and checked before use:
int *p = nullptr; if (p != nullptr) cout << *p; // safe: skips if null
Dereferencing a null or invalid pointer crashes the program, so this check matters.
Pointers and functions
Passing a pointer lets a function change the caller's variable (like pass-by-reference):
void addOne(int *p) { (*p)++; }
int main() {
int n = 5;
addOne(&n);
cout << n; // 6
}6
Pointers and arrays
An array's name acts like a pointer to its first element, so pointers can walk an array:
int a[3] = {10, 20, 30};
int *p = a; // points to a[0]
cout << *p << " " << *(p + 1); // 10 20Common mistakes
- Dereferencing an uninitialised or null pointer — a crash.
- Using a dangling pointer after its memory is freed or out of scope.
- Confusing
*p(the value) withp(the address). - Forgetting the parentheses in
(*p)++vs*p++.
Declare an int, make a pointer to it, print the value both directly and via the pointer, then change the value through the pointer. Write a swap(int*, int*) function that swaps two numbers.
Summary
- A pointer stores the address of another variable.
&gets an address;*dereferences to the value.- Set unused pointers to
nullptrand check before dereferencing. - Pointers let functions modify the caller's data and walk arrays.
- For everyday tasks, references are often safer than raw pointers.
Frequently Asked Questions
What is a pointer in C++?
*, such as int *p;, and it lets you read or change the variable it points to through its address.What is the difference between the * and & operators in C++?
& address-of operator gives the memory address of a variable, so &x is "the address of x." The * dereference operator goes to the address a pointer holds and accesses the value there, so *p is "the value p points to."What is nullptr in C++?
nullptr is a special value meaning a pointer points to nothing. It is the modern, type-safe replacement for the older NULL, and checking if (p != nullptr) before using a pointer helps avoid crashes from dereferencing an invalid address.What is a dangling pointer in C++?
nullptr after freeing.What is the difference between a pointer and a reference in C++?
* to access the value. A reference is an alias for an existing variable, cannot be null or reseated, and is used like the variable itself. References are safer for most everyday tasks.