By the end of this lesson, cadets will be able to:
struct types to group related data. (Outcome 2)enum and typedef to create readable user-defined types. (Outcome 2)Complete before class. These set the vocabulary we will use.
| Source | Sections | Why |
|---|---|---|
| Beej-C | Chapter 8 (Structs) | Defining structs, dot operator, initialization |
| Beej-C | Chapter 10 (typedef) | Skim. Focus on aliasing struct types; you will use this in the walkthrough and lab. |
| King, C Programming: A Modern Approach | Chapter 16 (Structures, Unions, and Enumerations) | Optional deeper coverage of enum and typedef |
=. You can skim the section on pointers to structs and the arrow operator; that is Lesson 19.3 99. When you write b = a, C copies every field of a into b. Once that copy is done, a and b are two separate variables. Changing b.x leaves a.x untouched. This is one of the two ideas the lesson hinges on.Block 3 is about building the tools for serious programs. Today is the foundational tool: a way to design your own types. Up to now you have used the types the language gave you (int, double, char, pointers, arrays). Starting today, you describe the data your program needs and the compiler treats your description as a real type. Lesson 19 takes the same struct and points at it; Lesson 20 organizes structs across files. Today is the rock at the base.
The Lesson 18 deck covers why structs exist, the syntax of defining and using them, enum and typedef, and the struct copy semantic. The slides are the spine of the lecture; the struct walkthrough is where you practice each idea hands-on.
You already know how to pass several related values around: a function takes three ints and a double. That works for small programs, but it scales badly. When the program grows, the same group of values appears in many places, and the meaning of each parameter has to be remembered from the function name alone. A struct lets you name the group once and treat it as a single value from then on.
In Python you used dictionaries and class instances for this. C has nothing as flexible as a dictionary, but it does give you the next best thing: a type you define yourself, with named fields and known sizes. The compiler reserves a single block of memory for the whole group and lets you reach into it by name.
A struct definition introduces a new type. The type has a tag (the name after the word struct) and a list of fields inside braces. Each field has its own type and its own name. End the definition with a closing brace and a semicolon.
This does not allocate any memory yet. It declares a type, the same way int is a type. You can declare as many variables of this type as you want, or none. Field names are local to the struct: another struct can have its own field called hp with no conflict.
To make a variable of a struct type, write the whole type name (including the word struct) followed by the variable name. To read or write a field, use the dot operator.
The dot operator picks one field out of the bundle. pikachu.hp reads as "the hp field of the pikachu variable". The same syntax works for assignment and for reading. Heads up: declaring a struct without an initializer leaves all its fields uninitialized, exactly like an uninitialized primitive from Lesson 2.
You can set all fields in one statement at declaration time. Two forms:
Prefer designated initializers in your own code. They survive field reorderings, they let you skip fields you do not care about (skipped fields are zero-initialized), and a reader can see the field name at the call site instead of counting commas.
An enum is a list of named integer constants. The compiler hands out values starting from 0 unless you say otherwise. Enums pair naturally with structs: a struct field has a known set of allowed values, and the enum members are the readable names for them.
Style note: write the member names in SCREAMING_SNAKE_CASE. They live in the same namespace as ordinary names; the all-caps convention signals "this is a constant".
Writing struct Pokemon everywhere gets old fast. typedef lets you give an existing type a second name and use that second name interchangeably.
Style note: typedef names use the _t suffix. The standard library follows this rule (size_t, time_t, pid_t) and matching it makes your code look like it belongs in the language.
Real-world C code typedef-s almost every struct so that the word struct rarely appears outside the type definitions themselves.
_t suffix (so pokemon_t, not PokemonType or pokemon). §5.5 requires enum constants to use SCREAMING_SNAKE_CASE (so TYPE_FIRE, not type_fire or Fire). The full standard lives in the course Resources section; both rules apply to Lab 18 and every C lab after this one.
This is the surprise of Lesson 18 and the bridge to Lesson 19. When you write b = a; for two struct variables of the same type, C copies every field of a into the corresponding field of b. The two variables are now equal but they are SEPARATE: changing one does not affect the other.
This is the opposite of what Python does with object assignment. In Python, b = a would point both names at the same object; changing one would visibly change the other. In C, structs are values, and values get copied. Tomorrow in Lesson 19 we will see what this means when you pass a struct to a function as an argument and why C programmers usually pass a pointer to a struct instead of the whole thing.
Three things to do in class. Use the checklist to track your progress.
Twelve tasks across five sections. Define a struct Pokemon, declare variables of it, set fields, use designated initializers, alias the type with typedef, then watch what happens when you copy one struct into another.
Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Lesson 18 reflection.