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
EasyWrite a C program that demonstrates the equivalence between array names and pointers to their first elements. Your program should:
- Declare an integer array named
numberswith 4 elements and initialize it with the values {10, 20, 30, 40} - Declare a pointer to an integer named
ptr - Assign the array name
numbersto the pointerptr - Print the address stored in
numbersusingprintfwith the%pformat specifier - Print the address of the first element
&numbers[0]usingprintfwith the%pformat specifier - Print the address stored in
ptrusingprintfwith the%pformat specifier - Print the value of the first element using the array name:
*numbers - 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: 10This 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 elementAccess values using pointer notation:
*numbers // Value of first element via array name
*ptr // Value of first element via pointerKey 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;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions