Course contentsShow
← C
Lesson 476 of 1,2197. PointersFree lesson

Pointer to the start of an array

Pointer to the start of an array

What you'll learn: How to use an array's name as a shortcut to its starting memory address.

The Secret Identity of Arrays

In C, arrays and pointers have a very close relationship. When you declare an array like int numbers[5];, you aren't just creating a row of five integers; you are also creating a convenient way to find the very first element.

Here is the "magic" rule: The name of an array acts as a pointer to its first element.

Imagine a row of houses on a street. If the entire row is called "Oak Street," the name "Oak Street" is also the address of the very first house on the block. In C, if you use the name numbers without any square brackets, the compiler treats it as the memory address of numbers[0].

Seeing the Address

Because the array name is essentially a pointer, you can assign it directly to a pointer variable. You don't even need the "address-of" operator (&) because the name itself is already an address.

#include <stdio.h>

int main() {
    int ages[3] = {21, 30, 45};
    
    // Create a pointer and point it to the start of the array
    // We don't need &ages, just the name 'ages'
    int *ptr = ages;

    // These two lines print the exact same memory address
    printf("Array address:   %p\n", (void*)ages);
    printf("Pointer address: %p\n", (void*)ptr);

    // Accessing the first value using the pointer
    printf("First element:   %d\n", *ptr);

    return 0;
}

Why This Matters

This concept is why you can pass a large array to a function without copying the whole thing. Instead of handing the function a thousand integers, you just hand it the "start" address (the array name).

However, there is one small catch: while a pointer is a variable that can be changed to point elsewhere, an array name is a constant pointer. You can use ages to find the start of your data, but you cannot reassign ages to point to a different array later on. It is permanently tied to the beginning of its block.

Key takeaway

An array's name is simply a pointer to its first element (index 0).