Menu
Coddy logo textTech

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 30

This 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 icon

Challenge

Easy

Write a C program that demonstrates accessing array elements using pointer notation instead of traditional array indexing. Your program should:

  1. Declare an integer array named values with 6 elements and initialize it with the values {5, 15, 25, 35, 45, 55}
  2. Use a for loop to iterate through all elements of the array
  3. Inside the loop, use pointer notation *(values + i) to access each element (do not use bracket notation like values[i])
  4. Print each element using the pointer notation access method
  5. 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]

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: 25

This 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 30

C 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;
}
quiz iconTest yourself

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

All lessons in Logic & Flow