Stage 10: Pointers, Address-of, and Dereference Operators
Understanding the foundations of memory addressing in C++: how pointers and low-level operators work.
Pointers: The Key to Understanding Computer Memory
Pointers are one of the most powerful and demanding elements of the C++ language. Unlike regular variables that store specific values (like numbers or characters), pointers store addresses in RAM. Understanding them is essential for working with dynamic memory allocation, arrays, and advanced data structures.
1. The Address-of Operator (&) and Pointer Declaration
Every variable in a program occupies a specific spot in memory. To find out the address where a variable is located, we use the address-of operator &.
Declaration
To declare a pointer, we use the asterisk symbol * with the data type. The pointer's type must match the type of data it points to.
int number = 100;
int *ptr; // Declaration of a pointer to an integer
ptr = &number; // Assigning the address of 'number' to the pointer2. The Dereference Operator (*)
Once we have a pointer holding an address, we usually want to access the value stored at that address. We use the dereference operator * for this.
Note: The * symbol has two meanings:
- In declaration:
int *ptr;means "I am creating a pointer." - In code:
*ptr = 20;means "go to the address in ptr and write 20 there."
#include <iostream>
int main() {
int x = 10;
int *p = &x;
std::cout << "Address of x: " << p << std::endl;
std::cout << "Value at address p: " << *p << std::endl; // Prints 10
*p = 50; // Changing x's value via the pointer
std::cout << "New value of x: " << x << std::endl; // Prints 50
return 0;
}3. Null Pointer: nullptr
A pointer that hasn't been initialized contains a random address ("garbage"). Attempting to dereference such a pointer almost always results in a Segmentation Fault. Therefore, it is best practice to initialize a pointer with nullptr.
int *p = nullptr; // Pointer points to nothing
if (p != nullptr) {
std::cout << *p;
}4. Pointer Arithmetic and Arrays
Arrays and pointers are closely related in C++. An array's name is actually a pointer to its first element.
tabis the address&tab[0]*(tab + 1)is the same astab[1]
We can move a pointer along an array by adding integers to it. C++ automatically calculates the byte offset based on the data type (e.g., for int, adding 1 moves the address by 4 bytes).
int numbers[] = {10, 20, 30};
int *ptr = numbers; // Points to numbers[0]
std::cout << *ptr << " "; // 10
std::cout << *(ptr + 1) << " "; // 20Exam Pitfalls
- Uninitialized Pointers: Never do
*p = 5;unless you have assignedpthe address of an existing variable or dynamic memory. - Confusing Operators:
&- give me the address.*- enter this address.
- Pointers vs. References: A reference is just an alias (another name), whereas a pointer is a separate variable storing an address. A pointer can be changed during execution to point to something else—a reference cannot.
Example: Function Using a Pointer
Instead of a reference, we can use a pointer to modify a value in a function:
void setZero(int *p) {
if (p != nullptr) {
*p = 0;
}
}
int main() {
int a = 100;
setZero(&a); // We pass the address of variable a
// a is now 0
}You might also like
Stage 11: Pointer Arithmetic in C++
Understanding how C++ operates on memory addresses. Learn why ptr++ is more than just adding one.
Stage 3: Automatic One-Dimensional Arrays in C++
A guide to static one-dimensional arrays: declaration, initialization, and safe iteration.
Stage 6: C-Style String Handling (char[])
A guide to low-level string processing as null-terminated character arrays, following exam constraints.