You are looking at lesson 10. Your place is still lesson 8, task 1: Memory.
A copy or a pointer, the -> operator, const T * for reading, an array of structures, searching with strcmp and sorting by swapping whole structures.
A structure is a value just like an int: you can pass it to a function, return it from one and put it in an array.
void grow_copy(City c, int count) {
c.population += count; /* changes the copy */
}
void grow(City *c, int count) {
c->population += count; /* changes the original */
}
grow_copy(kaunas, 1000); /* kaunas didn't change */
grow(&kaunas, 1000); /* kaunas + 1000 */
As with int (topic 8): a value passed in is a copy. For a function to change the original, it gets a pointer.
c->population is shorthand for (*c).population: "the member of what c points to". The brackets are required because . binds tighter than *. With a pointer you always write ->.
Even when a function doesn't change the structure, it's usually passed by pointer:
void print_city(const City *c);
const means nothing can be written through c. The compiler checks it, and a reader sees from the header straight away that the function only reads.The rule: a structure goes to a function as const T * when it's only read, and as T * when it's changed.
#define CITY_COUNT 5
City cities[CITY_COUNT] = {
{"Klaipeda", 150000, 98.0},
{"Vilnius", 600000, 401.0},
...
};
printf("%s\n", cities[1].name); /* Vilnius */
print_city(&cities[i]); /* the address of the i-th */
cities[i] is a structure, cities[i].population its member. You pass the array to a function as in topic 6: City t[] and n.
A linear search (topic 6), only the comparison uses strcmp:
for (int i = 0; i < n; i++) {
if (strcmp(t[i].name, name) == 0) {
return i;
}
}
return -1;
Selection sort (topic 6) is the same; only two places change:
t[j].population > t[max].population or, by name, strcmp(t[j].name, t[min].name) < 0;City temp:City temp = t[i];
t[i] = t[max];
t[max] = temp;
Because the whole structure travels, the population and area stay with their city. If you kept three separate arrays, sorting would have to swap all three – and one day you'd forget one.
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.