1. Intro
Arrays and pointers are closely related in C++. In most expressions, an array name automatically "decays" into a pointer to its first element. This close relationship is why array elements can be accessed both with the familiar arr[i] syntax and with pointer notation *(arr + i) — they compile to the same thing.
Cricket analogy: A scoreboard listing overs by number (arr[i]) is really the same as pointing at the start of the innings and counting forward i overs (*(arr + i)) — both land on the same over, like referencing Rohit Sharma's fourth over two different ways.
2. Syntax
int arr[5] = {1, 2, 3, 4, 5};
int* p = arr; // same as: int* p = &arr[0];
arr[i]; // subscript notation
*(arr + i); // equivalent pointer notation
*(p + i); // also equivalent, via p3. Explanation
When an array arr is used in most expressions (as a function argument, assigned to a pointer, used in arithmetic), it decays into a pointer to its first element: arr behaves the same as &arr[0]. This is why int* p = arr; compiles without needing &arr[0] explicitly. Because of this decay, indexing an array with arr[i] is defined in the language as shorthand for *(arr + i) — first move the pointer i elements forward using pointer arithmetic, then dereference it. Both forms give identical results and even allow the unusual-looking but valid i[arr], since addition is commutative. One key difference: unlike a real pointer, the array name itself is not a modifiable variable — you cannot reassign arr to point elsewhere, though a pointer copy of it (p) can be reassigned freely.
Cricket analogy: A commentator saying "the sixth over" (arr[5]) means the same as "start at the first over and count five more" (*(arr + 5)); the innings itself can't be reassigned to start elsewhere, but a bookmark copy of it can be moved freely.
4. Example
#include <iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int* p = arr; // array decays to pointer to arr[0]
for (int i = 0; i < 5; i++) {
cout << "arr[" << i << "] = " << arr[i]
<< " | *(arr+" << i << ") = " << *(arr + i)
<< " | *(p+" << i << ") = " << *(p + i) << endl;
}
cout << "arr == &arr[0]? " << (arr == &arr[0]) << endl;
return 0;
}5. Output
arr[0] = 10 | *(arr+0) = 10 | *(p+0) = 10
arr[1] = 20 | *(arr+1) = 20 | *(p+1) = 20
arr[2] = 30 | *(arr+2) = 30 | *(p+2) = 30
arr[3] = 40 | *(arr+3) = 40 | *(p+3) = 40
arr[4] = 50 | *(arr+4) = 50 | *(p+4) = 50
arr == &arr[0]? 16. Key Takeaways
- An array name decays to a pointer to its first element in most expressions, so
arrbehaves like&arr[0]. arr[i]is defined as shorthand for*(arr + i); both notations are interchangeable.- Pointer arithmetic scaling (from
sizeofthe element type) is what makesarr + iland on the correct element. - Unlike a pointer variable, the array name itself cannot be reassigned to point elsewhere.
Practice what you learned
1. In most expressions, what does an array name `arr` decay into?
2. Which expression is equivalent to `arr[i]`?
3. What is the value of `arr == &arr[0]` for an array `arr`?
4. Can you write `int* p; p = arr;` to make `p` point to the first element of `arr`?
5. What is a key difference between an array name and a pointer variable that points to it?
Was this page helpful?
You May Also Like
Pointer Arithmetic in C++
Understand how adding or subtracting integers from a pointer moves it by whole elements, scaled by the size of the pointee type.
Arrays in C++
Learn what an array is in C++, how to declare and access a fixed-size collection of same-type elements, and why zero-based indexing matters.
Passing Arrays to Functions in C++
Understand how arrays decay to pointers when passed to C++ functions, why size must be passed separately, and how this differs from pass-by-value.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics