Section A.8 Arrays
A C array is a fixed number of values of one type, stored next to each other in memory. Its length is set when it is declared, and its elements are numbered from 0, as in a Python list:
int counts[4]; // four ints: counts[0] to counts[3]
uint8_t steps[3] = {1, 2, 4}; // three values, given when declared
counts[0] = 7;
Much of what a Python list does is not available. An array cannot grow, so there is no
append. There is no len(), no counting from the end with a negative index and no slicing, and one array cannot be assigned to another with =.
Most importantly, C does not check an index.
counts[4] compiles and runs, and reads or overwrites whatever memory comes after the array, where Python would stop with an IndexError. Keeping the length in one #define and using that name in every loop over the array ensures that the declaration and every loop use the same length:
#define N_READINGS 4
int readings[N_READINGS];
for (int i = 0; i < N_READINGS; i++) {
readings[i] = 0;
}
