Array names as constant pointers
What you'll learn: How to treat an array's name as a fixed memory address that points to its first element.
The "Fixed Address" Concept
In C, when you declare an array like int numbers[5];, the compiler does something very clever. It sets aside a block of memory for five integers, and it treats the name numbers as the memory address of the very first integer in that block.
Think of an array like a house with a very long porch divided into sections. The name of the array is like the GPS coordinate for the front door. It tells you exactly where the structure starts. Because the rest of the sections are attached directly to the front door, knowing where the door is means you know where the entire house is.
The Constant Behavior
The most important thing to understand is that an array name is a constant pointer. This means that while you can use it to look at data, you cannot change where the "name" points.
If you create a regular pointer, you can point it at x, then later point it at y. However, an array name is "married" to its memory block. You cannot reassign it. If you try to do numbers = &some_other_int;, the compiler will throw an error. The front door of the house cannot be moved to a different street!
Seeing it in Code
Because the array name acts as a pointer to the first element, the following two expressions are identical in C:
#include <stdio.h>
int main() {
int prices[3] = {10, 20, 30};
// 'prices' behaves like a pointer to the first element
printf("Address of first element: %p\n", (void*)prices);
printf("Address using & operator: %p\n", (void*)&prices[0]);
// You can even use the dereference operator (*) on the array name
printf("First value: %d\n", *prices); // Outputs 10
// IMPORTANT: You cannot do this:
// int other = 50;
// prices = &other; // Error! Array names are constant.
return 0;
}
Why this matters
C treats arrays this way to be efficient. Instead of passing an entire copy of a 1,000-element array into a function, C simply passes the address of the first element (the array name). By understanding that prices is just a pointer to prices[0], you unlock the ability to navigate through memory quickly and efficiently.
Key takeaway
An array name is a fixed pointer to its first element; you can read its address, but you can never change where it points.