In C and C++, arrays and pointers are intimately linked. An array's name acts as a constant pointer holding the base address of its first element (index 0). Understanding how arrays decay into pointers is essential for mastering C++ systems programming.
#include <iostream>
int main() {
int numbers[4] = {100, 200, 300, 400};
// All three lines produce the EXACT same value:
std::cout << "numbers[2]: " << numbers[2] << "\n"; // Subscript syntax -> 300
std::cout << "*(numbers+2): " << *(numbers + 2) << "\n"; // Pointer arithmetic -> 300
std::cout << "2[numbers]: " << 2[numbers] << "\n"; // Valid C++! -> 300
return 0;
}
"What is the difference between an Array of Pointers and a Pointer to an Array?"
• int* arr[5];: An array of 5 pointers to integers.
• int (*ptr)[5];: A single pointer that points to an entire array of 5 integers.
Boost your C++ proficiency by studying these closely connected core modules:
Array definitions and memory allocation.
Pointer arithmetic and dereferencing.
Avoiding array decay with modern C++ containers.