How to use arrays

Create and initialize an array

An array stores a fixed number of same-type elements contiguously. Unspecified initializer elements become zero.

int scores[5] = {90, 82, 75, 88, 0};

Calculate its length

In the same scope as an actual array, divide total bytes by one element's bytes. This does not work after the array has decayed to a pointer.

size_t count = sizeof scores / sizeof scores[0];

Visit valid indexes

Indexes begin at zero and end at count - 1. Reading or writing past either boundary is undefined behavior.

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

Pass a pointer and count

Functions receiving arrays need an explicit element count. Validate externally supplied indexes before using them.

if (index < count) {
    scores[index] = new_score;
} else {
    fprintf(stderr, "Index out of range\n");
}