Wednesday, December 16, 2009

Why we need Pointers - An Everyday Analogy using Trains/Telephone Number

Everyday Analogy

1) We regularly use pointers in everyday life when we use telephone numbers.
2) Another notable use is in chains and trains (concrete examples of linked lists). A chain is a group of things linked together by malleable joints. These joints are easily changeable when adding/removing new links/compartments to the chain.

Note : 
 a)Telephone no.s give 2-way access whereas a pointer gives only 1-way access.
 b)Usually the no. of links is 1 to 3 in an access path. 

Tom ---> Jerry
Tom ---> 1 ---> Jerry
Tom ---> 1 ---> 2 ---> .... ---> N ---> Jerry
Here Tom calls Jerry by
a)Getting the contact no. directly or indirectly from 'a friend-of-a-friend-of-a-friend'.
b)Dialling the number.
Here the intermediate links of the chain can change the destination similar to an operator diverting your call to the concerned person in an organisation. Say Tom wants some info (from Jerry), but N on the path knows that Mortimer is a better source of info and gives Mortimer's no. instead.
a)Original path:
Tom ---> 1 ---> 2 --->...N --->Jerry
b)Redirection:
Tom ---> 1 ---> 2 --->...N ---> Mortimer
This is similar to shifting tracks for a train, we can easily divert the train to whatever track by using the shunting mechanism.
----
Note:
I'd contributed this article as a section on the wikipedia page for C/C++ pointers, but some time later it disappeared in the rather over zealous editing by the page maintainers who totally mangled it into bits and threw the ashes to the winds. I had to rescue it from the edit history spending a few hours to track down my original post and now it has a permanent home here. I was really gratified to get one comment from one of the reviewers there (talk section). Really made my day that I'd managed to simply convey the heart of pointers to a non-programmer in terms they could easily identify with, understand and even explain to some one else. Why we need pointers at all in the first place.
Good Pointer Fun with Binky Video from Stanford University.