6
$\begingroup$

How would you explain pointers and their use to high school students with some prior knowledge in programming, mainly in Python, but very limited knowledge of C?

Note that this isn't a duplicate of Simple Pointer Examples in C, because here I'm asking for an explanation of how they work, not an example of them working.

$\endgroup$
  • 1
    $\begingroup$ Please clarify what you mean by a explanation that does not include an example. Otherwise, this may be a dup. My answer in cseducators.stackexchange.com/questions/299/… contains an explanation of an example. $\endgroup$ – hotpaw2 Jun 17 '17 at 20:06
  • $\begingroup$ Does this have to be in C? C is heavily criticized for its design wrt memory management, pointers in particular. If you could avoid using C, there are plenty of languages where some of the problems endemic to it have been avoided, for example, OCaml, Pascal, Go, even Forth to some degree. $\endgroup$ – wvxvw Jun 19 '17 at 11:22
8
$\begingroup$

A prerequisite to understanding pointers is a basic understanding of memory. At least on the broadest level, students need to know that when they declare a variable in their programs, that variable is stored somewhere, so they can use it, access it, update it, etc. Where that variable is stored also has some address, so it can be located (specific addressing techniques need not be introduced yet). The specifics of heap v. stack, memory size (int v. long long), and other such concepts are not necessary to understand what a pointer is (but they certainly would help).

All this background established, here is the easy way to think of pointers:

A pointer is a variable that contains an address of a variable.

Simple enough on the surface. That sentence comes from Chapter 5 of K&R (The C Programming Language by Kernighan and Ritchie). This chapter has a great explanation of pointers and arrays, and I would highly recommend it for an understanding of pointers, at least in the context of C. One key point for C is the following:

Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function. ... Pointer arguments enable a function to access and change objects in the function that called it.

Now, to get into the details of pointer arithmetic, passing by reference, syntactic sugar vis-a-vis arrays, pointers do become more complicated. However, always keep in mind the simple definition that gets at their essence: pointers store addresses for other locations in memory.

$\endgroup$
  • 2
    $\begingroup$ I am tempted to say that all programming books are "footnotes to K&R". Students absolutely have to understand how a computer works in the way you describe, otherwise they would be no better off than a surgeon who does not know about blood circulation. $\endgroup$ – user737 Jun 18 '17 at 13:55
5
$\begingroup$

I set the stage from the beginning. When we first do variables, I draw a big box and call it the computers memory and then draw a small box of memory and say "it's at some address" (having already explained that memory is set up so you can identify each piece with a number) so when we say:

x = 20 (or int x = 20 or whatever)

I draw something like:

x
100
____
|   |
|   |
-----

explaining that we have a block of memory at location 100 large enough to store an int (in this case) and we can get to it using the identifier x.

then

x=10 takes the 10 and stores it in the memory

and

a = x + 5 looks to see that x is an identifier to memory 100, we get the 10 then add 5 and then store it in the box referred to by a (which is at memory location 108.

x         a
100       108  
____      ____
|   |     |   |
|10 |     | 20|
-----     -----

This sets the stage for pointers later on. When we get to pointers, we merely have to explain that the box holds a number that refers to another memory address or location:


*p             i
100            108
-------        ------
| 108 |        | 49 |
|     | -----> |    |
-------        -------

all the while using lots of drawings and arrows.

Also, when we talk about functions, I don't formally describe the stack but talk about it and then the memory for parameters and locals is in a subsection of memory allocated for that function call.

$\endgroup$
1
$\begingroup$

Pointers are simply variables which are used to store memeory addresses of another variables. for example suppose we have a integer variable int a =10; then the value 10 i.e. is stored at a memory cell which can be accessed with the help of a pointer. With the help of pointer we can directly access the value the variable to which the pointer is pointing.

$\endgroup$
  • 1
    $\begingroup$ Welcome to CSE! OP isn't confused about the material. He is asking how you might explain this to students so that a classroom full of kids will remember and understand. $\endgroup$ – Ben I. Jun 19 '17 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.