A Pointer is a variable that stores the physical hexadecimal memory address of another variable in RAM. Pointers give C++ programmers unparalleled power to directly inspect and manipulate hardware memory with maximum execution speed.
&): Retrieves the physical RAM address of a variable.*): Accesses or modifies the value stored at the address pointed to by the pointer.#include <iostream>
int main() {
int target = 42;
int* ptr = ⌖ // ptr stores the memory address of target
std::cout << "Value of target: " << target << "\n"; // 42
std::cout << "Memory address (&target): " << &target << "\n"; // e.g., 0x7ffd98
std::cout << "Address stored in ptr: " << ptr << "\n"; // 0x7ffd98
std::cout << "Dereferenced value (*ptr):" << *ptr << "\n"; // 42
// Modifying the value via pointer dereference:
*ptr = 100;
std::cout << "New value of target: " << target << "\n"; // 100!
return 0;
}
When you increment a pointer (ptr++), C++ does NOT add 1 byte. Instead, it advances the pointer by sizeof(T) bytes, matching the size of the underlying data type:
int arr[] = {10, 20, 30};
int* p = arr;
std::cout << *p << "\n"; // 10
p++; // Moves forward by sizeof(int) = 4 bytes!
std::cout << *p << "\n"; // 20
Differentiate between Wild Pointer and Null Pointer:
• Wild Pointer: An uninitialized pointer containing random garbage memory bits. Accessing it crashes the OS.
• Null Pointer: Explicitly points to address zero (nullptr), representing that it intentionally owns no object.
Boost your C++ proficiency by studying these closely connected core modules:
Compare direct aliasing vs indirect addressing.
How array names decay into pointer offsets.
Modern RAII wrappers that eliminate raw pointer leaks.