You are looking at lesson 6. Your place is still lesson 10, task 1: struct.
An array in memory, indexes from 0, a constant size, initial values, going out of bounds, sizeof, and an array as a parameter.
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 */
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.
#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.
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.
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 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 *'.
void read_temperatures(int t[], int n) { ... }
double average(const int t[], int n) { ... }
n.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.
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.