LearnCodeVisually

Arrays in C Explained Visually: Index, Read and Update

Choose an index, read its value and change it in a five-element C array. Learn why indexing starts at zero and why bounds matter.

Explore the full interactive C arrays lesson →

Start with working C code

Five integers occupy indexes 0 through 4. Before the assignment, scores[1] is 20; afterward its value is 25.

#include <stdio.h>
int main(void) {
    int scores[5] = {10, 20, 30, 40, 50};
    scores[1] = 25;
    printf("%d\n", scores[1]);  /* prints 25 */
    return 0;
}

Interactive array-indexing visualization

Use the controls to follow the C example above. The labels describe the current model state.

The explanatory material on this page is also available without JavaScript. Enable JavaScript to use the visual step controls.

Ready to explore the example.

What is an array in C?

An array is a fixed-length sequence of elements of the same type. For int scores[5], legal indexes are 0 through 4. scores[0] names the first element; scores[4] names the fifth.

Elements are stored contiguously. Each valid index identifies an element directly, so accessing it requires constant-time address calculation. The diagram uses logical boxes, not actual operating-system memory addresses.

Why array bounds matter

Unlike some languages, ordinary C array indexing does not automatically check bounds. Accessing scores[5] or scores[-1] is an out-of-bounds access and has undefined behavior. It might appear to work or it might crash, but neither result makes it safe.

When iterating over all five elements, use for (int i = 0; i < 5; ++i). Compare the index and value visually to understand which element each iteration reads.

Practice with indexing

  1. Find the element at index 3 and predict its value before changing anything.
  2. Change the element at index 1 to 25, then read it again.
  3. Explain why scores[5] is invalid, even though the array contains five elements.
Answers

The initial element at index 3 is 40. After the update, index 1 contains 25. Index 5 would refer to a sixth element, but only indexes 0 through 4 are valid.

Open the full visual arrays lesson →