Loading lessons...
Pointers & Arrays
Pointers & Arrays
Arrays and pointers are nearly twins in C: an array name "decays" to a pointer to its first element.
The array name is a pointer
int a[] = {10, 20, 30};
int *p = a; // same as &a[0]
printf("%d", *p); // 10, the first element
Pointer arithmetic moves between elements
a[0] -> *p
a[1] -> *(p + 1)
a[2] -> *(p + 2)
Adding 1 to the index moves one element forward (not one byte!).
The classic equivalence
a[i] is the same as *(a + i). Welcome to pointer math.
TL;DR
- The array name acts as a pointer to its first element.
p + ipoints to element i.- To index:
a[i]and*(a + i)are identical. - Increments move by element size, not by byte.