Pointers and memory in C lesson plan
What is really in 2 KB of RAM, and the idea that makes C what it is: addresses, pointers, arrays and strings by hand, structs, the stack, function pointers. Ends with a serial command line built from all of it.
Objectives and materials
By the end, students can
- Addresses in the Uno's 2 KB, then pointers that hold them.
- Arrays as pointers, then strings walked by hand.
- Structs and the arrow, then where locals and statics live.
- Function pointers, then a command parser built from everything.
Materials for the whole unit
- Arduino Uno R3
- Full-size breadboard
- LED x 3
- Pushbutton
- Resistor x 3
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Where things live
Addresses in the Uno's 2 KB, then pointers that hold them.
| Lesson | Time | What students do |
|---|---|---|
| AddressesEvery variable lives at a numbered byte: where globals sit in the Uno's 2 KB, neighbors in an array, the bytes of a number stored little end first, and the map from 0x100 to 0x8FF. | 40 min |
|
| PointersA variable that holds an address: declaring one, reading and writing through it with *, swap done right, NULL and the check before you follow it, and a button that fires at whatever the pointer aims at. | 40 min |
|
Materials
- Arduino Uno R3
- Full-size breadboard
- LED x 3
- Pushbutton
- Resistor x 3
Check for understanding
Each student finishes "Pointers" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "NULL: pointing at nothing".
Arrays and text
Arrays as pointers, then strings walked by hand.
| Lesson | Time | What students do |
|---|---|---|
| Arrays are pointersAn array's name turns into the address of its first element: pointer arithmetic that counts in elements, a[i] as *(a + i), walking an array with a pointer, the sizeof trap in a function, and a lamp chaser that is a moving pointer. | 40 min |
|
| Strings by handA C string is chars and a zero byte: length, upper case and reverse written with pointers, a buffer overflow you can watch trample its neighbor, the bounded copy that stops it, and a compare that lights a lamp. | 40 min |
|
Materials
- Arduino Uno R3
- Full-size breadboard
- LED x 3
- Pushbutton
- Resistor x 3
Check for understanding
Each student finishes "Strings by hand" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Compare by hand".
Structs and the stack
Structs and the arrow, then where locals and statics live.
| Lesson | Time | What students do |
|---|---|---|
| Structs and pointersA fleet of ships in an array of structs: sizeof with no padding on the AVR, the arrow operator, a copy against a pointer, a search that returns NULL, a button that flies, and a fourth ship. | 40 min |
|
| Stack and staticWhere things live in 2 KB: globals low, the stack high and growing down with every call, a local that forgets against a static that remembers, and a buffer that eats the free RAM. | 30 min |
|
Materials
- Arduino Uno R3
- Full-size breadboard
- LED x 3
- Pushbutton
- Resistor x 3
Check for understanding
Each student finishes "Stack and static" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Spend the free RAM".
Code as data
Function pointers, then a command parser built from everything.
| Lesson | Time | What students do |
|---|---|---|
| Function pointersA pointer to code: one call that runs add or multiply, a sort that takes its order as a function, a callback for each element, and a table of lamp modes, with a police mode you add yourself. | 40 min |
|
| Project: a command parserEverything in one program: a bounded line buffer, a tokenizer that splits in place into argv, a number parser, lamps found by name through a table, and a table of commands with function pointers. Add your own command, then make it yours. | 45 min |
|
Materials
- Arduino Uno R3
- Full-size breadboard
- LED x 3
- Pushbutton
- Resistor x 3
Check for understanding
Each student finishes "Project: a command parser" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Make it yours".
Teach this unit as a class
Start a class from this course and its 8 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.