Skip to content
CurriculumChapter 11 · Arrays & Strings
BeginnerHigh focus~14 min

One-Dimensional Arrays

An array is a contiguous block of same-typed elements. That layout is why indexing is fast and why an out-of-bounds index is dangerous.

In plain English

An array is a row of numbered lockers. The index is the locker number.

01

Collections and array declarations

An array declaration gives the element type, a name, and a size: int scores[5]; creates five ints. The array is C's built-in homogeneous collection: every element has the same type. Structures and unions are the aggregate alternatives, and arrays can contain them. The array name refers to the whole collection, while an index selects one element. The index type is an integer, and the index variable is the value used to select an element. The position of an element is its index, starting at zero.

chapter-11.cC
int scores[5];       /* five ints */
scores[0] = 90;
scores[4] = 75;

for (int i = 0; i < 5; i++) {
    printf("%d ", scores[i]);
}
02

Referring to individual elements

Use the array name plus an index in brackets to read or write one element. Arrays are not checked at runtime, so the programmer must ensure the index is within bounds. Entering data into an array and reading it back usually happens in a loop. The array does not remember its length for you, so pass the length separately to functions.

chapter-11.cC
for (int i = 0; i < 5; i++) {
    scanf("%d", &scores[i]);
}

for (int i = 0; i < 5; i++) {
    printf("%d\n", scores[i]);
}
03

The array type and the size option

The declaration int scores[5] gives the array the type int[5]: the element type is int, and the size is part of the type. The name scores refers to the whole array. sizeof(scores) is the size of the whole array, sizeof(scores[0]) is the size of one element, and their quotient is the element count. When you initialize an array, you can omit the size and let the compiler infer it from the initializer list: int a[] = {1, 2, 3}; creates three elements.

chapter-11.cC
int scores[5];

printf("%zu\n", sizeof scores);                 /* whole array */
printf("%zu\n", sizeof scores[0]);              /* one element */
printf("%zu\n", sizeof scores / sizeof scores[0]); /* 5 */

int inferred[] = {1, 2, 3};                     /* size 3 */

Watch it happen

Step through the memory changes one instruction at a time.

step-by-step

Step 1 of 6

Create the array

int a[4] = {10, 20, 30, 40}; stores four ints contiguously. Each int is 4 bytes here.

a[0] · 0x7ffc2000
10
a[1] · 0x7ffc2004
20
a[2] · 0x7ffc2008
30
a[3] · 0x7ffc200c
40
a[0]0x7ffc2000→10

Key takeaways

  • Arrays store same-typed elements contiguously.
  • Indexing starts at 0 and C does not bounds-check it.
  • The array name refers to the whole collection.
  • The size is part of the array type; sizeof(array) / sizeof(array[0]) gives the element count.
  • When initializing, omit the size to let the compiler infer it.

Common pitfalls

  • Accessing one past the last element.
  • Confusing the number of elements with the highest valid index.
  • Using an uninitialized index variable.
  • Dividing sizeof(pointer) by sizeof(element) after the array decays.

Key focus

  • Trace array loops carefully.
  • Know the valid index range for an array of size n.
  • Expect questions on sizeof(array) versus sizeof(pointer).

Quick check

0 / 2 answered

1. What are the valid indexes for int a[5];?

2. What does the array name represent in most expressions?