Accessing arrays with pointer notation
What you'll learn: How to use pointer arithmetic to navigate and read array elements without using square brackets.
The Secret Identity of Arrays
In C, an array's name is actually a constant pointer to its first element. Think of an array like a train sitting on a track. The name of the array is the GPS coordinate of the very first car. If you have int numbers[3], the word numbers is simply the memory address of numbers[0].
Because the array name is a pointer, we can use the "dereference" operator (*) to see what’s inside. Writing *numbers is exactly the same as writing numbers[0].
Moving Down the Line
If numbers points to the first item, how do we get to the second? We use pointer arithmetic. By adding 1 to the address, we tell C to look at the next memory slot.
Think of it like a house address. If you are standing at #100 Main St (the start of the array), adding 1 moves you to #101 Main St (the next element).
Here is how the two notations compare:
numbers[0]is the same as*(numbers + 0)numbers[1]is the same as*(numbers + 1)numbers[2]is the same as*(numbers + 2)
Seeing it in Code
When we add an integer to a pointer, C is smart. It knows how big an int or a char is, so it jumps the perfect number of bytes to land exactly at the start of the next element.
#include <stdio.h>
int main() {
int prices[3] = {10, 20, 30};
// Traditional way
printf("First item: %d\n", prices[0]);
// Pointer way
printf("First item: %d\n", *prices);
// Moving to the second item using pointer math
// We wrap (prices + 1) in parentheses so we move
// the address BEFORE we look inside it.
printf("Second item: %d\n", *(prices + 1));
return 0;
}
Why Use Pointers?
You might wonder why we would use *(prices + 1) when prices[1] is easier to read. While square brackets are great for humans, computers view arrays as pointers. Understanding this "under-the-hood" math allows you to pass arrays to functions efficiently and perform high-speed data processing that standard indexing sometimes hides.
Key takeaway
An array name is a pointer to its first element, and you can access any element by adding an offset to that pointer and dereferencing it.