Pointer Arithmetic for Grandma

greenspun.com : LUSENET : Joel on Software : One Thread

I just read "The Guerrilla Guide to Interviewing" along with a number of other pieces by Joel. Good stuff!

So here's a challenge. Joel, etal

Explain pointers in C so your Grandma can understand! Explain complex doubly-indirected thinking in a context that would allow one to realize when one is doing so.

And of course "Smart people don't need these things explained!" is very, very, very lame.

-- Anonymous, April 11, 2001

Answers

I'm not Joel, but what's wrong with describing pointers by first describing computer memory as a series of boxes which can hold stuff, and each box has a label, or 'address.' Because you can put any kind of different stuff in each of these boxes, they are called 'variables,' i.e. the stuff in them can vary. One of the things you could put in a box would be the label of another box. When a box holds another box's label it is called a 'pointer variable' because the label inside it 'points' to another box.

My grandma, were she alive, would understand boxes and labels. She packed for a move or two.

Could she then write a C program? Of course not. But writing a C program is not prerequisite to understanding pointers.

Bob

-- Anonymous, April 13, 2001


I usually try to explain pointers using examples from the US Postal Service. People already understand mailboxs, addresses, and their physical/logical relationship.

For example, I recently moved, so I told my old post office to forward my new mail to my new post office. People who don't know my current address can still send me mail. Now if I move again in less then a year and forward my mail from my current post office to yet another, then that's a real-life example of double indirection. btw, I don't know if the US Postal Service allows such multiple forwarding..??

When explaining indirection, I try using a PO Box as an example. If I people send mail to my PO Box, they don't know when or where I've moved, but I still receive their mail. I don't have to send out those annoying change-of-address cards. That's a C code optimization that also has value in real-life! ;-)

-- Anonymous, April 14, 2001


The box and mail metaphors work for describing pointers as ways of finding stuff, and of generalising the process of handling whatever is in the boxes or the mail pigeon holes.

It doesn't help for pointer arithmetic though.

Now that we have languages like Java which abstract pointers so that pointer arithmetic isn't available perhaps that isn't so important and of course the reason why Java does that is that programmers used to screw up pointer arithmetic all the time.

-- Anonymous, April 18, 2001


I agree that the postal analogy is a good place to start when describing indirection. A slightly different approach works very well with pointer arithmetic.

Once Grandma has the label/variable/pointer thing down cold, ask her to consider the following. She has a street address that's associated with a particular building, say 100 Main. Now you ask her to tell you the address of the building next to it. She might guess 102 Main (or 98). But, you say, it could just as well be 110, 116, 102b, 200, etc. Wouldn't it be great if you could tell what the street address of the building next door, or five doors down, was just by looking at your address? . Then tell Grandma, that using pointers in C, you can. You specify a street address, you simply say 2 buildings down, or 5 buildings up, and you end up with the right address. The computer figures out what the addresses are in between so that you don't have to worry about skipped numbers, and apartments, office buildings, etc. If you want, this system lends itself to an understanding of various pitfalls: NULL => blank address => kaboom. You could even try to describe malloc() as a request to build a certain number of buildings; free() bulldozes the buildings, so you can't use an address that has been free()'d etc...

Don't blame me if Grandma asks you to use pointer arithmetic to send her mail :)

-- Anonymous, April 19, 2001


i think pointers are like riding a bicycle. once u get it, u never forget. anyone agree?

im a completely self-taught programmer, including pointers. of coz after i discovered BBSs and Internet, i learnt alot more from others. but in my experience, learning and writing assembly can help your pointer skills incredibly. just an idea...

-- Anonymous, April 19, 2001



Moderation questions? read the FAQ