Menu
Coddy logo textTech

Iterator Pattern

Part of the Object Oriented Programming section of Coddy's C journey — lesson 56 of 61.

The Iterator pattern provides a way to access elements of a collection sequentially without exposing its underlying structure. Instead of giving users direct access to an array, you give them an object that knows how to step through the data one element at a time.

An iterator typically needs two pieces of information: a reference to the collection and the current position. In C, we create a struct to hold this state:

typedef struct {
    int* data;      // pointer to the array
    int size;       // total number of elements
    int current;    // current position
} IntIterator;

The iterator exposes two key functions. has_next() checks if there are more elements to read, and next() returns the current element and advances the position:

int has_next(IntIterator* it) {
    return it->current < it->size;
}

int next(IntIterator* it) {
    return it->data[it->current++];
}

Using the iterator looks clean and hides the array details:

IntIterator it = create_iterator(numbers, 5);
while (has_next(&it)) {
    printf("%d\n", next(&it));
}

The caller never needs to know that numbers is an array or how indexing works. This abstraction makes it easy to change the underlying data structure later — from an array to a linked list, for example — without changing the code that uses the iterator.

challenge icon

Challenge

Easy

Let's build a NumberList iterator — a clean abstraction that lets you traverse a collection of integers without exposing the underlying array structure.

You'll organize your code across three files:

  • iterator.h: Define an IntIterator struct that holds a pointer to an integer array, the total size of the collection, and the current position. Declare three functions: create_iterator (takes an array pointer and size, returns an initialized iterator), has_next (checks if more elements remain), and next (returns the current element and advances the position). Don't forget include guards!
  • iterator.c: Implement your iterator functions. The create_iterator function should return an IntIterator by value with the current position set to 0. The has_next function returns 1 if there are more elements to read, 0 otherwise. The next function returns the element at the current position and then increments the position.
  • main.c: Read the number of elements, then read each integer value into an array. Create an iterator for this array, then use a while loop with has_next and next to traverse and print each element on its own line.

Your program will receive:

  1. The number of elements in the array
  2. Each integer value on a separate line

Use the iterator pattern to print all elements — no direct array indexing in your traversal loop!

Example output when inputs are 4, then 10, 20, 30, 40:

10
20
30
40

Example output when inputs are 3, then -5, 0, 100:

-5
0
100

Example output when inputs are 1, then 42:

42

The iterator hides how the data is stored — your main loop simply asks "is there more?" and "give me the next one" without knowing anything about array indices or memory layout.

Cheat sheet

The Iterator pattern provides a way to access elements of a collection sequentially without exposing its underlying structure.

An iterator struct holds the collection reference, size, and current position:

typedef struct {
    int* data;      // pointer to the array
    int size;       // total number of elements
    int current;    // current position
} IntIterator;

The iterator exposes two key functions:

int has_next(IntIterator* it) {
    return it->current < it->size;
}

int next(IntIterator* it) {
    return it->data[it->current++];
}

Using the iterator hides array implementation details:

IntIterator it = create_iterator(numbers, 5);
while (has_next(&it)) {
    printf("%d\n", next(&it));
}

This abstraction allows changing the underlying data structure without modifying client code.

Try it yourself

#include <stdio.h>
#include "iterator.h"

int main() {
    int n;
    scanf("%d", &n);
    
    int arr[n];
    
    // TODO: Read n integer values into the array
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    
    // TODO: Create an iterator for the array
    
    // TODO: Use a while loop with has_next and next to traverse
    // and print each element on its own line
    // Do NOT use direct array indexing in your traversal loop!
    
    return 0;
}
quiz iconTest yourself

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

All lessons in Object Oriented Programming