Pointers let you work directly with memory addresses. But if used wrong, they can cause bugs or crashes.
When you want to share data between functions without copying it.
When you need to work with dynamic memory (memory you create while the program runs).
When you want to build complex data structures like linked lists or trees.
When you want to optimize performance by avoiding copying large data.
When you need to interact with low-level system or hardware features.
Syntax
C++
type *pointerName;
// Example:int *p;
A pointer stores the address of a variable of the specified type.
The asterisk (*) declares a pointer variable.
Examples
Declare a pointer to int and set it to null (no address).
C++
int *p; // pointer to int
p = nullptr; // pointer points to nothing
Pointer p stores the address of variable x.
C++
int x = 10;
int *p = &x; // pointer holds address of x
Trying to use *p without setting p first causes a crash.
C++
int *p;
*p = 5; // ERROR if p is uninitialized
Sample Program
This program shows how to use a pointer correctly and checks before dereferencing to avoid mistakes.
C++
#include <iostream>
int main() {
int x = 42;
int *p = &x; // p points to x
std::cout << "Value of x: " << x << "\n";
std::cout << "Value via pointer p: " << *p << "\n";
p = nullptr; // p points to nothing// Common mistake: dereferencing nullptr causes crashif (p != nullptr) {
std::cout << *p << "\n";
} else {
std::cout << "Pointer p is null, cannot dereference.\n";
}
return0;
}
OutputSuccess
Important Notes
Never use a pointer before assigning it a valid address.
Always check if a pointer is null before dereferencing it.
Be careful with pointers to memory that has been freed or deleted.
Summary
Pointers store addresses and must be initialized before use.
Dereferencing a null or uninitialized pointer causes crashes.
Always check pointers before using them to avoid common mistakes.
Practice
(1/5)
1. What happens if you try to dereference a pointer that has not been initialized in C++? int *p; int x = *p;
easy
A. It causes undefined behavior and may crash the program.
B. It automatically initializes the pointer to zero.
C. It assigns zero to the variable x.
D. It prints the address stored in p.
Solution
Step 1: Understand pointer initialization
The pointer p is declared but not initialized, so it points to an unknown memory location.
Step 2: Dereferencing uninitialized pointer
Dereferencing *p tries to access memory at an undefined address, causing undefined behavior or crash.
Final Answer:
It causes undefined behavior and may crash the program. -> Option A
Quick Check:
Uninitialized pointer dereference = crash [OK]
Hint: Never use pointers before initializing them [OK]
Common Mistakes:
Assuming uninitialized pointers are null
Thinking dereferencing gives zero
Ignoring pointer initialization
2. Which of the following is the correct way to initialize a pointer to null in C++?
easy
A. int *p = NULL;
B. int *p = nullptr;
C. int *p = '0';
D. int *p = 0x0;
Solution
Step 1: Understand null pointer initialization
In modern C++, nullptr is the preferred way to initialize a pointer to null.
Step 2: Check other options
NULL is old style (macro), 0x0 is a literal but less clear, and '0' is a char, not a pointer.
Final Answer:
int *p = nullptr; -> Option B
Quick Check:
Use nullptr for null pointers [OK]
Hint: Use nullptr for null pointers in modern C++ [OK]
Common Mistakes:
Using character '0' instead of nullptr
Using integer 0 instead of nullptr
Confusing NULL macro with nullptr
3. What is the output of the following code?
int x = 10; int *p = &x; *p = 20; std::cout << x << std::endl;
medium
A. 10
B. Address of x
C. Compilation error
D. 20
Solution
Step 1: Understand pointer assignment
Pointer p stores the address of x. So *p refers to x.
Step 2: Modify value through pointer
Assigning *p = 20; changes the value of x to 20.
Final Answer:
20 -> Option D
Quick Check:
Pointer dereference changes original variable [OK]
Hint: Changing *p changes the original variable [OK]
Common Mistakes:
Confusing pointer value with pointed value
Expecting original variable unchanged
Thinking *p prints address
4. Identify the error in the following code snippet:
int *p = nullptr; std::cout << *p << std::endl;
medium
A. Missing semicolon after nullptr.
B. Pointer p is not declared.
C. Dereferencing a null pointer causes runtime error.
D. std::cout cannot print pointer values.
Solution
Step 1: Analyze pointer initialization
Pointer p is initialized to nullptr, meaning it points to nothing.
Step 2: Dereferencing null pointer
Trying to access *p causes a runtime error (crash) because it points to no valid memory.
Final Answer:
Dereferencing a null pointer causes runtime error. -> Option C
Quick Check:
Dereference nullptr = crash [OK]
Hint: Never dereference nullptr or uninitialized pointers [OK]
Common Mistakes:
Assuming nullptr can be dereferenced safely
Ignoring runtime crash possibility
Confusing pointer declaration with usage
5. Given the code below, what is the safest way to avoid a dangling pointer?
int *p = new int(5); delete p; // What should be done next?
hard
A. Set p to nullptr after delete.
B. Assign p to a new integer immediately.
C. Do nothing; pointer is automatically safe.
D. Use delete[] p; instead of delete p;.
Solution
Step 1: Understand delete effect
After delete p;, the pointer p still holds the old address but memory is freed (dangling pointer).
Step 2: Prevent dangling pointer
Setting p = nullptr; makes it clear pointer is invalid and safe to check before use.
Final Answer:
Set p to nullptr after delete. -> Option A
Quick Check:
Nullify pointer after delete to avoid dangling [OK]
Hint: Nullify pointers after delete to avoid dangling [OK]
Common Mistakes:
Using delete[] for single object
Not setting pointer to nullptr after delete
Assuming pointer auto-clears after delete
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.