Structures and dynamic memory
Grouping related values, naming constants, asking for memory at run time, and building data structures from nodes.
About 14 hours of study
Learning objectives
- Define and use structs
- Use const, enum and macros
- Allocate and free heap memory
- Build and free a linked list
Lessons
- 1
Structures Intermediate
Group related values with struct and typedef, arrays of structs, and the -> operator.
- 2
Constants, enums and macros Intermediate
Give values names with const, enum and #define, write safe macros, and understand what the preprocessor does before compiling.
- 3
Dynamic memory: malloc and free Intermediate
Ask for memory at run time on the heap, give it back with free, and avoid leaks and dangling pointers.
- 4
Linked lists Intermediate
Nodes on the heap joined by next pointers: build, walk, insert at the front, and free.
Module practice
- C: Distance between two pointsEasy
- C: Top studentMedium
- C: Sort an array of studentsHard
- C: A named constantEasy
- C: A traffic lightMedium
- C: Repair the macrosHard
- C: An average on the heapEasy
- C: Store n numbers on the heapMedium
- C: An array that growsHard
- C: Add up a linked listEasy
- C: Add to the end of a listMedium
- C: A stack as a linked listHard