C++ Pointers
What Is a Pointer in C++?
A pointer in C++ is a special variable designed to store the memory address of another variable instead of holding a direct value. Rather than keeping data itself, a pointer acts like a locator, pointing to where the actual data is placed in memory.
Imagine a pointer as a house key. The key (pointer) doesn’t contain your belongings, but it tells you where they are.
Why Use Pointers?
- Enables indirect access to data stored elsewhere
- Allows efficient data manipulation without making copies
- Used for function arguments to reflect changes in original variables
- Supports dynamic memory allocation via new and delete
- Helps manage linked structures like linked lists, trees, and graphs
Basic Pointer Syntax
type* pointerName;
- type: The kind of data the pointer will address (e.g., int, float)
- *: Declares that it’s a pointer
- pointerName: Your chosen label for the pointer
Simple Example of a Pointer
#include <iostream>
int main() {
int number = 42;
int* ptr = &number;
std::cout << "Original value: " << number << "\n";
std::cout << "Address of number: " << &number << "\n";
std::cout << "Pointer value (address stored): " << ptr << "\n";
std::cout << "Value pointed to: " << *ptr << "\n";
return 0;
} Pointer Terminology in Plain Words
| Term | Fresh Meaning |
|---|---|
| * | Dereference symbol – accesses pointed data |
| & | Address-of operator – gets location |
| ptr | Label for memory holder |
| *ptr | Data retrieved from stored address |
Changing Value Using Pointer
#include <iostream>
int main() {
int x = 10;
int* px = &x;
*px = 99; // Modify the actual value via pointer
std::cout << "Modified x: " << x << "\n";
return 0;
} - Altering *px updates x directly, since both point to the same memory space.
Pointers in Functions
Pointers can pass variables by reference, so that changes inside a function affect the original data:
#include <iostream>
Void changeValue(int* n) {
*n = 77;
}
int main() {
int data = 55;
changeValue(&data);
std::cout << "New value: " << data << "\n";
return 0;
} - changeValue modifies data via pointer — no duplication of memory happens.
Dynamic Memory with Pointers
Pointers are essential when allocating memory during runtime:
#include <iostream>
int main() {
int* p = new int; // allocate space
*p = 88;
std::cout << "Dynamic value: " << *p << "\n";
delete p; // release memory
return 0;
} - new gives memory during execution.
- delete prevents memory leakage by freeing used space.
Pointers vs Regular Variables
| Regular Variable | Pointer Variable |
|---|---|
| Holds data | Holds reference to data location |
| Directly accessed | Requires dereferencing to view actual value |
| No need for * | Uses * and & for interaction |
Summary
Pointers give C++ the ability to directly work with memory, offering faster performance and deeper control over how data is stored and changed. From altering variables without copies to building advanced data systems like trees or allocating memory only when required, pointers make C++ flexible and powerful.
They don’t store the data themselves — they guide your code to where the data actually lives.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand C++ Tutorial visually:
What You'll Learn:
- 📌 C++ pointers explained easy
- 📌 POINTERS in C++