Past the End
What happens when you use an index where there is no element, such as index 5 in an array of five elements? That depends on whether the index is a number written in the code, or a variable.
When the index is a number, the compiler sees the mistake, and the program doesn't compile:
#include <stdio.h>
int main(void) {
int scores[5] = {80, 92, 75, 60, 88};
printf("%d\n", scores[5]);
return 0;
}main.c:5:20: error: array index 5 is past the end of the array (that has type 'int[5]') [-Werror,-Warray-bounds]
5 | printf("%d\n", scores[5]);
| ^ ~
main.c:4:5: note: array 'scores' declared here
4 | int scores[5] = {80, 92, 75, 60, 88};
| ^
1 error generated.The message says that index 5 is past the end of the array, whose type is int[5]: an array of five ints. The lines below it, starting with note:, show where the array was created. A negative index is a mistake too: for scores[-1], the message says that index -1 is before the beginning of the array.
When the index is a variable, the compiler doesn't check it, because the variable only gets its values while the program runs. This is what happens when a loop goes one step too far, with <= instead of <:
#include <stdio.h>
int main(void) {
int scores[5] = {80, 92, 75, 60, 88};
for (int i = 0; i <= 5; i++) {
printf("%d\n", scores[i]);
}
printf("Done\n");
return 0;
}80
92
75
60
88
RuntimeError: unreachableThe program prints all five elements. In the sixth iteration of the loop, i is 5, and at scores[5] the program stops with the red error RuntimeError: unreachable. Done never gets printed. The message doesn't say what went wrong, but in this lesson, it means an index outside an array. The last line that got printed tells you how far the program got.
The same happens when the count in a loop is bigger than the array, for example const int COUNT = 6; for an array of five elements.
Why does this matter so much? C itself doesn't check indexes at all. A normal C program on your computer would read whatever lies in memory after the array, maybe another variable, and carry on with a wrong value. Writing to scores[5] could even overwrite another variable. Such a program can seem to work and still give wrong results, and the mistake is very hard to find. Here, the program stops at such an index, so that you see the mistake right away. But the best protection is to get your indexes right: the first element has index 0, the last one size − 1, and a loop over an array has i < COUNT in its condition.
Instructions
The program should print the times of four laps. Run it: it prints three laps and stops with RuntimeError: unreachable. In the last iteration of the loop, i is 4, and laps[4] is past the end of the array. Fix the condition of the loop so that the loop ends before i reaches 4. The program then prints Finished at the end.
Now the program gets to the end, but the first lap is missing: Lap 1 shows 58, the time of the second lap. Make the loop start at index 0, and print the lap number as i + 1. The output is then:
Lap 1: 62 s
Lap 2: 58 s
Lap 3: 60 s
Lap 4: 57 s
FinishedStart programming for free
5/12