When you were three years old, you asked “why” an awful lot. If you have kids you’re probably sick and tired of hearing that question. But it is a fundamental question; it’s how we learn. We have a need to understand. Some things are learned over time, like “We need to eat so we don’t die.”
But some things are not easily understood.
Like Pointers in C.
Recently I came across an exercise that was designed to help the student understand the use of pointers in C. Here it is:
Declare three doubles. Then write a function that takes in a double, followed by pointers to the other two doubles. The function should add one to each double. Call the function and print out the values of each. What do you observe?
This is instructor focused. It screams “I need you to understand pointers. We’ll do a trivial example that shows that the first parameter won’t be modified but the other two will.” It’s terse and leaves a lot of work up to the student. It’s good to make students work, but you must give them motivation before you just throw them into the deep end of the pool.
If we just added a little context and some explanation, we could make this better and actually explain what’s going on:
You need to write code to move a game piece from one location to another on a two dimensional board. To do so, define a ‘move’ function for this. The ‘move’ function should take in four arguments: the current position of the piece on the x axis, the current position of the piece on the y axis, the number of spaces to move on the x axis, and the number of spaces to move on the y axis. Since you can only return one value from our function, define this ‘move’ function as void, and use pointers to modify the original x and y position of the piece. Think about how you will modify those existing x and y positions.
Both use the same premise: in C, we can’t return more than one result from a function, so we get around that by using pointers to pass values by reference so we can change the values of the arguments we pass in. This is often called mutation. In some languages, mutation is bad. In others, it’s the way stuff gets done. Context is key.
But this second example is more learner-focused. It gives meaningful context to the problem at hand. It explains what we’re doing, and the context helps the student understand why. It _ specifically reinforces_ the idea that if we want a function to modify multiple values, we have to use pointers and modify those values.
This example could be vastly improved depending on what specific things you needed to teach.
What’s not helpful, though, is using simplistic, out of context exercises. A demo where you have the student print out memory addresses for variables does nothing to motivate them. Why do they care about the memory address? What’s its significance? You know the reason why they should care.
Articulate that to them. And if they’re not ready to understand it, then it’s not time to introduce the topic yet. Teaching the basics is hard. Be patient, be articulate, and help students connect the dots.
They’ll get there, and so will you.