Menu
Coddy logo textTech

Array Names as Pointers

Part of the Logic & Flow section of Coddy's C journey — lesson 7 of 63.

Arrays and pointers in C have a special relationship that's fundamental to understanding how C works with data in memory. When you declare an array, the array name itself acts as a constant pointer to the first element of the array.

This means that when you have an array like int numbers[5];, the name numbers is essentially a pointer that always points to numbers[0]. You can think of the array name as a built-in pointer that you don't need to declare separately.

int numbers[5] = {10, 20, 30, 40, 50};
// 'numbers' points to the first element
// numbers == &numbers[0]  // These are equivalent!

This equivalence is crucial: numbers and &numbers[0] both give you the exact same memory address. The array name is just a more convenient way to refer to the address of the first element without having to use the address-of operator.

Understanding this relationship opens the door to using pointer techniques with arrays, which can make your code more flexible and efficient. The key difference is that unlike regular pointers, array names are constant pointers - you cannot change what they point to.

challenge icon

Challenge

Easy

Write a C program that demonstrates the equivalence between array names and pointers to their first elements. Your program should:

  1. Declare an integer array named numbers with 4 elements and initialize it with the values {10, 20, 30, 40}
  2. Declare a pointer to an integer named ptr
  3. Assign the array name numbers to the pointer ptr
  4. Print the address stored in numbers using printf with the %p format specifier
  5. Print the address of the first element &numbers[0] using printf with the %p format specifier
  6. Print the address stored in ptr using printf with the %p format specifier
  7. Print the value of the first element using the array name: *numbers
  8. Print the value of the first element using the pointer: *ptr

Your output should display the results in the following format:

Array name address: [address]
First element address: [address]
Pointer address: [address]
Value via array name: 10
Value via pointer: 10

This challenge demonstrates that the array name acts as a constant pointer to the first element, and that numbers, &numbers[0], and ptr all contain the same memory address when ptr is assigned the array name.

Cheat sheet

Array names in C act as constant pointers to the first element of the array.

When you declare an array, the array name points to the first element:

int numbers[5] = {10, 20, 30, 40, 50};
// 'numbers' points to the first element
// numbers == &numbers[0]  // These are equivalent!

You can assign an array name to a pointer:

int *ptr;
ptr = numbers;  // ptr now points to first element

Access values using pointer notation:

*numbers  // Value of first element via array name
*ptr      // Value of first element via pointer

Key difference: Array names are constant pointers - you cannot change what they point to, unlike regular pointers.

Try it yourself

#include <stdio.h>

int main() {
    // TODO: Write your code here
    // 1. Declare and initialize the array 'numbers' with {10, 20, 30, 40}
    // 2. Declare a pointer to integer named 'ptr'
    // 3. Assign the array name to the pointer
    // 4. Print all the required addresses and values
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow