Theory
Common mistakes
Structure bugs are mostly about what is a copy and what is the original.
Common mistakes
.instead of->.c.populationwhencis a pointer:'c' is a pointer; did you mean to use '->'?. A value gets., a pointer gets->.*c.population..binds tighter than*, so this means*(c.population). Writec->populationor(*c).population.- Changing a copy. A function
void f(City c)changes its own copy; the caller doesn't see the change. When the function must change it –City *candf(&kaunas). - Passing a structure by value for no reason.
void print_student(Student s)copies 164 bytes on every call. For reading –const Student *s. ==on structures.invalid operands to binary ==. Compare the members, strings withstrcmp.=on an array member.s.name = "Ona";givesassignment to expression with array type. Copy withsnprintf. (=does work on a whole structure.)- Swapping only one member. If sorting swapped only
name, the grades would belong to someone else. Swap the whole structure throughT temp. - Counting the size by hand.
char+intisn't 5 but 8 bytes, because of alignment. Find the size withsizeof. - A forgotten semicolon after
}.typedef struct { ... } Citywithout;givesexpected ';' before 'int': the message names a word from the next line (int main), although the gap is here.