Skip to the task
Layout
Back to classic view
READTask 1 of 28 · step 1: ArraysArrays
Arrays in practice/01/

An array in memory, indexes from 0, a constant size, initial values, going out of bounds, sizeof, and an array as a parameter.

Details & hint

Why an array

A week of temperatures would need seven variables: t1, t2, … t7. The average would be a sum of seven terms, and a month would take thirty-one variables. A loop doesn't help: t1 and t2 are different names, and a loop can't take "the next variable".

An array is one name for many values of the same type. Each value is reached by its index, the position number in brackets:

int t[7];      /* 7 whole numbers one after another */
t[0] = 3;      /* the first */
t[6] = 6;      /* the last */
t 3-258-146? t[0]t[1]t[2]t[3]t[4]t[5]t[6]t[7] 7 boxes, indexes 0 to 6someone else's memory

Indexes start at 0, so the last one is t[6], not t[7]. An array's boxes sit in memory one after another, with no gaps.

The size is a constant

#define DAYS 7

int temperatures[DAYS];

#define DAYS 7 tells the compiler to put 7 wherever DAYS is written, before compiling. The size is written in one place, and the loops use it: for (int i = 0; i < DAYS; i++). Names in capitals are a convention that says: this is a constant.

int n = 7; int t[n]; is allowed by the C17 standard, but the course flag -Wvla turns it into an error: ISO C90 forbids variable length array 't' (GCC words it that way even with -std=c17). The reason: such a variable-length array can't be initialized (variable-sized object may not be initialized), and an n that is too big breaks the program without any message. An array's size is always a constant.

Initial values

int t[7] = {3, -2, 5};   /* t[3]..t[6] get 0 */
int times[11] = {0};    /* all 11 boxes are 0 */
int targets[] = {0, 998, 1998, 7};   /* size 4, from the number of values */

An array with no initial values (int t[7]; inside a function) holds garbage: whatever was in that memory before. Fill it first, then read it.

Out of bounds

t[7] in a seven-box array is no longer the array: it is someone else's memory. The compiler usually doesn't warn about it, and on Windows the program often seems to work:

for (int i = 0; i <= 7; i++) {   /* <= instead of < */
    t[i] = 100 + i;
}

This mistake (an eighth write into a seven-box array) passes silently in CLion, even though it writes a value somewhere else. GitHub CI builds with sanitizers, and there the same loop stops with the message:

.../main.c:7:10: runtime error: index 7 out of bounds for type 'int [7]'

The file, line and column, then what happened: index 7 is past the end of an array of seven int. You learn to read longer sanitizer reports in topic 12.

sizeof

sizeof t is how many bytes the whole array takes, sizeof t[0] is one box. Divide them and you get the number of boxes:

int count = (int)(sizeof targets / sizeof targets[0]);   /* 4 */

This only works where the array is declared. Inside a function the parameter is no longer an array, and the compiler warns: 'sizeof' on array function parameter 't' will return size of 'int *'.

An array as a parameter

void read_temperatures(int t[], int n) { ... }
double average(const int t[], int n) { ... }
  • The function doesn't know the size, so it comes as a separate parameter n.
  • Unlike an int (topic 5), an array is not copied: the function works on the same array, and whatever it writes, main sees too. Why that is becomes clear in topic 8 (pointers).
  • const int t[] is a promise that the function won't change the array. If you try t[i] = 0; anyway, the compiler stops you: assignment of read-only location.

One more: b = a; doesn't copy an array, it doesn't compile: assignment to expression with array type. You copy with a loop, box by box.

practice 01 CMakeLists.txt Code Diff
UNCHANGED This step does not change this file.
add_executable(p01_hello main.c)