Array Elements - Pointers
Part of the Logic & Flow section of Coddy's C journey — lesson 8 of 63.
Now that you understand how array names work as pointers, you can use this knowledge to access array elements in a completely different way. Instead of using the familiar bracket notation like array[i], you can use pointer notation to achieve the same result.
The key insight is that array[i] is exactly equivalent to *(array + i). When you write array + i, you're telling C to move i positions forward from the start of the array. The dereference operator * then gets the value at that location.
int numbers[5] = {10, 20, 30, 40, 50};
// These two lines do exactly the same thing:
printf("%d", numbers[2]); // Prints 30
printf("%d", *(numbers + 2)); // Also prints 30This pointer notation works because C automatically calculates the correct memory address based on the data type size. When you add 2 to an integer array pointer, C moves forward by 2 × sizeof(int) bytes, landing exactly on the third element.
Understanding this equivalence gives you flexibility in how you work with arrays and prepares you for more advanced pointer techniques that you'll encounter as you continue learning C.
Challenge
EasyWrite a C program that demonstrates accessing array elements using pointer notation instead of traditional array indexing. Your program should:
- Declare an integer array named
valueswith 6 elements and initialize it with the values {5, 15, 25, 35, 45, 55} - Use a for loop to iterate through all elements of the array
- Inside the loop, use pointer notation
*(values + i)to access each element (do not use bracket notation likevalues[i]) - Print each element using the pointer notation access method
- After the loop, demonstrate the equivalence by printing the third element (index 2) using both methods:
- First using pointer notation:
*(values + 2) - Then using traditional array notation:
values[2]
- First using pointer notation:
Your output should display the results in the following format:
Element 0: 5
Element 1: 15
Element 2: 25
Element 3: 35
Element 4: 45
Element 5: 55
Third element via pointer: 25
Third element via array: 25This challenge tests your understanding of the equivalence between array[i] and *(array + i), demonstrating how pointer arithmetic allows you to access array elements using memory address calculations instead of traditional indexing.
Cheat sheet
Array elements can be accessed using pointer notation instead of bracket notation. The expression array[i] is equivalent to *(array + i).
When you write array + i, C moves i positions forward from the start of the array. The dereference operator * gets the value at that location.
int numbers[5] = {10, 20, 30, 40, 50};
// These two lines do exactly the same thing:
printf("%d", numbers[2]); // Prints 30
printf("%d", *(numbers + 2)); // Also prints 30C automatically calculates the correct memory address based on the data type size. Adding 2 to an integer array pointer moves forward by 2 × sizeof(int) bytes.
Try it yourself
#include <stdio.h>
int main() {
// TODO: Write your code here
// Declare and initialize the array 'values' with 6 elements
// Use a for loop to print each element using pointer notation *(values + i)
// After the loop, print the third element using both pointer and array notation
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