Arrays
12 min lesson
- An array is a collection of values of the same type stored one after another in memory and accessed by a number called an index.
- Arrays matter because they let you store many related values under one name, like the scores of all students in a class.
- Think of an array as a row of numbered mailboxes. Each mailbox holds one item, and you reach a specific one by its number (index).
- The index is the position number; in C, the first element is at index 0, so a 5-element array uses indices 0 to 4.
- The size is fixed when the array is created and cannot change later.
- The computer reserves a block of memory big enough for all elements and places them next to each other.
- You read or write an element with square brackets: scores[0] = 90; or printf("%d", scores[0]);.
- The syntax is: type name[size] = { value1, value2, ... }; for example int scores[3] = {90, 85, 77};.
- A realistic use: storing monthly temperatures, student grades, or the pixels of a small image.
- A surprising detail: C does not check array bounds at runtime, so writing past the end corrupts memory silently.
- The example program stores five scores, prints them with a loop, and computes their average.
- Remember that indices start at 0, so the last index is size - 1.
- Never read or write outside the array bounds; it is undefined behavior.
- Use a loop to visit every element instead of writing it out by hand.
- Arrays cannot grow or shrink; choose a size that fits your data.
- Arrays unlock real data handling. Keep going, you are doing great!
#include <stdio.h>
int main(void) {
// An array of 5 scores, index 0 to 4
int scores[5] = {90, 85, 77, 92, 60};
// A running sum, starting at zero
int total = 0;
// Visit every element from index 0 to 4
for (int i = 0; i < 5; i++) {
// Print the score at position i
printf("Score %d: %d\n", i, scores[i]);
// Add it to the running total
total += scores[i];
}
// Compute the average with a cast so the division keeps decimals
double average = (double)total / 5;
// Print the average with two decimal places
printf("Average: %.2f\n", average);
return 0;
}What happens: Score 0: 90 Score 1: 85 Score 2: 77 Score 3: 92 Score 4: 60 Average: 80.80
Breaking it down
Array declaration
int scores[5] = {90, 85, 77, 92, 60};Creates 5 int boxes in a row and fills them with the listed values. The size 5 is fixed for the life of the array.
Accessing an element
scores[i]Reads the element at index i. The index must be between 0 and 4; anything else is out of bounds.
Loop over the array
for (int i = 0; i < 5; i++)Visits each index in turn. Using i < 5 avoids the out-of-bounds index 5.
Accumulating
total += scores[i];Adds each element to the running sum. By the end of the loop, total holds the sum of all scores.
Average
(double)total / 5Casts total to double so the division keeps decimals. Without the cast you would get an integer average.
Array basics
| Concept | Meaning | Example |
|---|---|---|
| Index | Position in the array | scores[0] |
| First index | Always 0 in C | scores[0] |
| Last index | size - 1 | scores[4] |
| Size | Number of elements | 5 |
| Out of bounds | Undefined behavior | scores[5] (bad) |
A helpful analogy
An array is like a row of numbered mailboxes. Each box holds one item, and the numbers start at 0. You open a specific box by its number, and all boxes are the same size and hold the same kind of item.
Nesting rules
- 1.The valid indices for an array of size n are 0 through n - 1.
- 2.Arrays cannot be resized after creation; choose the size carefully.
- 3.When passing an array to a function, the size is not carried along, so pass it as a separate argument.
- 4.You cannot assign one array to another with =; copy elements one by one or use memcpy.
Common mistakes
Using index 1 as the first element
Avoid
scores[1] // this is the second elementUse instead
scores[0] // this is the first elementC arrays start at index 0. Using 1 to mean 'first' shifts everything by one and can read the wrong value.
Looping one step too far
Avoid
for (int i = 0; i <= 5; i++) scores[i]Use instead
for (int i = 0; i < 5; i++) scores[i]The index 5 is out of bounds for a 5-element array. This is undefined behavior and can crash or corrupt data.
Trying to resize an array
Avoid
int a[3];
a = (int[]){1,2,3,4}; // errorUse instead
int a[4] = {1,2,3,4};The size is part of the array type. To hold more items, declare a bigger array or use dynamic memory later.
Quick tips
- •Use a named constant like #define SIZE 5 so the loop limit matches the array size.
- •Print the index along with the value while debugging to spot off-by-one errors.
- •Initialize arrays to avoid garbage values: int a[5] = {0}; fills all with 0.
- •Try changing one score and rerunning to see the average change.
- •Use sizeof(a) / sizeof(a[0]) to compute the element count of a local array.