-
Thinking of memory as a grid is really useful.
-
Arrays really have a single defining characteristic–elements are placed one after the other in a contiguous block of memory.
-
So if you know the starting address of the block, you just need to walk forwards from there and you’ll find each address holding an element of your array.
-
It’s helpful to keep track of the length of the array as you don’t wanna read memory you don’t own.
-
And this is exactly how arrays work in C. We are given a pointer to the first element of the array.
-
We can figure out the addresses of the other elements by adding to this address.
We can take this further
This is because a[n]
literally evaluates to *(a + n)
which is the same as *(n + a)
.