Last month, I discussed how to teach functions to students. This month I will be going over my main strategy for how to teach pointers to students. I think this one is quite a bit more unique, as I haven’t seen it anywhere else. It is definitely not very complicated though. If you have the code, which I will go over here, it’s quite easy. The basic idea is to help students think of pointers as just another data type, instead of worry about what memory addresses are, or how any of the super fine details work. I also will discuss how they have been using pointers for quite awhile already. Since, arrays and reference parameters are just pointers. If you couldn’t tell by the vocabulary I am using, this lesson will be using C++.

Introduction to how to teach pointers to students

I will assume during this article that you know what pointers are, and you have a good understanding of C++. I will be explaining most of the code though, so if you’re a bit rusty. This may be a good refresher for you.

So, the general idea is that I like to treat pointers as just another data type. Which, in a way, they are. If you write “int *x” you are not creating a normal integer variable. You are creating a variable which holds the memory address of an integer. Though, you should remember that they are technically not a data type. I like to help my students think about it like this because they already know variables quite well, and they are used to variables only holding certain types of data. So, telling them that a pointer can only hold memory addresses isn’t actually that hard for them to understand.

I then like to demonstrate this with what I believe is a very simple little program. When I am showing this to my students, I usually have it already written. Then, after introducing pointers, I ask them to guess what each line will do. The answers are always quite interesting.

First off, if you are just interested in the code and want to take a look at it for yourself. The entire program is right below this.

#include <iostream> using namespace std; int main() { int num = 7; int *ptrNum = # int y = 10; int *ptrToY = &y; //What will this output? cout << num << endl; //What kind of value will this output? cout << &num << endl; //What will this output? cout << ptrNum << endl; //Will this output the same as &num, or will it output 7? cout << *ptrNum << endl; //What will this output? cout << &ptrNum << endl; //Bonus questions *ptrNum = *ptrToY; //What will this output? cout << *ptrNum << endl; //Will this change? cout << &ptrNum << endl; system("pause"); }

In the next section I plan to go into more detail about how this code works. Then I will discuss how I use it to teach pointers. Remember that the main goal of this is just to demonstrate the syntax for how pointers work, and to give students practice thinking about them.

What the code is, and why it works.

I recommend you go ahead and look through the code above, if you can guess what everything should do, then you have a good understanding of pointers. I won’t discuss every single line as many don’t require an explanation. Though, I will explain the lines that I feel are definitely unconventional, or odd. You should see the following output, though keep in mind it may be different depending on what memory addresses your computer assigns.

7

00AFFA18

00AFFA18

7

00AFFA0C

10

00AFFA0C

The first two special lines are ones any student who has recently learned pointers should able to understand.

cout << &num << endl; cout << ptrNum << endl;

So, why are these the same? The answer is actually pretty straightforward. When we created “ptrNum”, we made it a pointer to an int. While this is 100% not a data type, I do find it helps to think along those lines. Now, let’s imagine that int* is a separate data type. Well, it would be a data type that holds a memory address. Also in C++, when we use the ampersand(&) operator it gives us the memory address for that variable. When you use “cout” it will output the value of that variable. So of course, “&num” gives us the memory address as the ampersand operator returns the address. Then “ptrNum” itself is equal to the memory address of “num”.



The next two lines I find quite interesting.

cout << *ptrNum << endl; cout << &ptrNum << endl;

Now, if you know pointers. You know that using the asterisk(*) operator will just give you the value of the variable that you are pointing to. What is quite interesting though is that using the ampersand operator on a pointer will give you the memory address of that pointer. This is a great way to illustrate that every variable has a memory address.

The last three lines are by far the trickiest.

*ptrNum = *ptrToY; cout << *ptrNum << endl; cout << &ptrNum << endl;

Now what is happening here is that we have made the value of “ptrNum” equal to “ptrToY” this means that “ptrNum” no longer points to our variable “num”. I like to use this example to show students that you can change the value a pointer is pointing to as easily as you can change the value of an integer variable. The next line simply proves to us that it worked. That line will output “10”. Which shows that it is now pointing to the variable “y”. The last line simply outputs the memory address of “ptrNum” again. This of course is going to be the same as when we outputted it at the beginning. That is because we merely changed the value that “ptrNum” was holding, we did not change the location of “ptrNum” at all.

Conclusion

I hope you enjoyed this post about how to teach pointers to students. I plan to do a few more posts in this series where I will talk about looping, variables, object oriented programming, and any other ideas I come up with. Hopefully, this becomes a popular series, because I have a lot of fun writing them.

If you are interested in other CodingOverload posts about programming. I recommend Resources for Teaching Competitive Programming & Taipei’s MRT Ridership Statistics