Basic Pointers in C++
Learn C++ pointers from scratch: store an address with &, read and change a value through a pointer with *, and see why ptr and &ptr print different addresses.
1#include <iostream>2using namespace std;34int main() {5 int num = 10;67 // Declare a pointer8 int* ptr;910 // Store address of num in pointer11 ptr = #1213 cout << "Value of num: " << num << endl;14 cout << "Address of num: " << &num << endl;15 cout << "Value of ptr (address stored): " << ptr << endl;16 cout << "Address of ptr: " << &ptr << endl;17 cout << "Value pointed by ptr: " << *ptr << endl;1819 // Modify value using pointer20 *ptr = 20;21 cout << "\nAfter modifying through pointer:" << endl;22 cout << "Value of num: " << num << endl;23 cout << "Value pointed by ptr: " << *ptr << endl;2425 return 0;26}
Sample Output
Value pointed by ptr: 10
Dereferencing ptr reads num.
Value of num: 20
Changed through *ptr = 20.
In this one
- What a pointer stores
- How & gets an address and * follows one
- Why changing *ptr changes the original variable
- The two meanings of * in declarations and expressions
How it works
- 1
num = 10.
- 2
ptr = &num stores num's address.
- 3
Print num, &num, ptr, &ptr, and *ptr.
- 4
*ptr = 20 changes num through the pointer.
- 5
Print num and *ptr again: both 20.
Edge cases
Uninitialized pointer
Remove ptr = # and print *ptr.
ptr holds garbage, so dereferencing it is undefined behavior: a crash or a random value. Initialize pointers, at least to nullptr.
Different addresses on each run
Run the program twice.
Operating systems randomize stack addresses for security, so the hex values change. Only the relationships (ptr equals &num) stay the same.
What's going on
A pointer is a variable that stores a memory address. This program makes one, points it at an int, reads the value through it, and then changes the int through it.
int num = 10; is a plain variable somewhere in memory.
int* ptr; declares a pointer to an int. Until it is given an address, it holds garbage, and reading through it would be undefined behavior.
ptr = # stores the address of num in ptr. The & here is the address-of operator.
The prints show four different things. num is 10. &num is where num lives (a hex address like 0x7fff5fbff6ac). ptr prints the same address, because that is what it holds. &ptr is a different address: the pointer is a variable too, so it lives somewhere of its own. *ptr is 10: the * in an expression means "go to the address and read what is there", called dereferencing.
*ptr = 20; writes through the pointer. Nothing assigns num directly, yet num now prints 20, because ptr and num refer to the same memory.
The * has two jobs, which confuses everyone at first. In a declaration (int* ptr) it means "this is a pointer". In an expression (*ptr) it means "follow the pointer".
Your addresses will differ from the sample output. They change from machine to machine and often from run to run.
The only header is <iostream>. It gives you cout for displaying output and cin for taking input from the user.
Understanding Pointers
Pointer Concept:
Memory Address:
Declaring Pointers
Syntax:
dataType* pointerName;
Example:
int* ptr; // Pointer to integer
What happens here:
Address Operator (&)
Getting Address:
int num = 10;
int* ptr = # // & gets address of num
What happens here:
Dereference Operator (*)
Accessing Value:
int value = *ptr; // * accesses value at address
What happens here:
Pointer Operations
Declaration and Initialization:
int num = 10;
int* ptr = #
Accessing Value:
cout << *ptr; // Prints 10
Modifying Through Pointer:
*ptr = 20; // Changes num to 20
Getting Address:
cout << ptr; // Prints address
cout << # // Same address
When to Use Pointers
Best For:
Example Scenarios:
Important Considerations
Initialization:
Null Pointers:
Dereferencing:
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
After int x = 5; int* p = &x; *p = 9; what is x?
Pick an answer to check it.
Question 2
What does &ptr give?
Pick an answer to check it.
Questions people ask
What is the size of a pointer?
Usually 8 bytes on 64-bit systems, no matter what type it points to.
Is int* p or int *p correct?
Both. The space is just style. Watch out for int* a, b; which makes only a a pointer.
What is a reference?
A reference (int& r = num) is another name for num. It cannot be null or reseated.