Lesson plan

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.

4 chapters, 8 lessons, about 5 hours, on the Arduino Uno R3. Every lesson runs in the browser, with nothing to install.

The unit

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.

Chapter 1. About 1 h 20 min

Where things live

Addresses in the Uno's 2 KB, then pointers that hold them.

LessonTimeWhat 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
  1. Run it: where fuel lives
  2. Neighbors in an array
  3. Little end first
  4. The map of 2 KB
  5. Wider boxes, wider gaps
  6. Quick check: predict, then read
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
  1. Run it: p holds an address
  2. Write through it
  3. Swap, done right
  4. Take hits
  5. Aim somewhere else
  6. NULL: pointing at nothing

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".

Chapter 2. About 1 h 20 min

Arrays and text

Arrays as pointers, then strings walked by hand.

LessonTimeWhat 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
  1. Run it: the name is the address
  2. Pointer arithmetic counts elements
  3. Walk it with a pointer
  4. The sizeof trap
  5. Start one along
  6. A chaser is a moving pointer
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
  1. Run it: three bytes for two letters
  2. strlen, with pointers
  3. Type a callsign
  4. Watch an overflow
  5. Copy with a limit
  6. Compare by hand

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".

Chapter 3. About 1 h 10 min

Structs and the stack

Structs and the arrow, then where locals and statics live.

LessonTimeWhat 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
  1. Run it: 13 bytes a ship
  2. The arrow
  3. A copy is a copy
  4. Find returns a pointer, or NULL
  5. Fly with the button
  6. A fourth ship
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
  1. Run it: low and high
  2. Every call goes deeper
  3. A counter that forgets
  4. static remembers
  5. Spend the free RAM

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".

Chapter 4. About 1 h 25 min

Code as data

Function pointers, then a command parser built from everything.

LessonTimeWhat 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
  1. Run it: op(6, 7)
  2. A sort that takes its order
  3. Sort the other way
  4. Callbacks
  5. A table of modes
  6. Add police mode
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
  1. Run it: a command line
  2. help reads the table
  3. Split in place
  4. Numbers from words
  5. Bad input, good answers
  6. Add your own command
  7. Make it yours

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".

Your class

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.