Lesson 21 / 36
Pointers & Arrays
An array name decays into a pointer to its first element.
Array name = address of element 0
In most expressions, an array's name decays into a pointer to its first element. That's why arr and &arr[0] are interchangeable, and arr[i] is really *(arr + i).
Same result, two notations
Indexing and pointer-offset dereferencing give identical results.
int arr[4] = {1, 2, 3, 4};
printf("%d\n", arr[2]);
printf("%d\n", *(arr + 2)); // same thing
Output:
3 3
But arrays aren't pointers
sizeof(arr) gives the whole array's byte size, while sizeof on a pointer gives just the pointer's size (usually 8 bytes). Decay only happens when the array is used, not when it's sizeof'd.