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.

Thursday, May 14, 2009

BookReview: Let Us C by Yeshavant Kanetkar

The book which opened the gates to so many others....
(Thanks to Bhallu and of course Yashavant Kanetkar).

The good thing about this book is
1) Easy to understand language
2) **** Diagrams to show visually things like pointer/array indexing.****.
Helps you to literally "see" whats going on inside your pointer based code.
3) The follow up books by the author let you dive deep into the C sea....

Thank you Yashavant Kantekar Sir!!

These are some of the other good books by him:

1) Understanding Pointers In C (Book) (very good understandable data-structures book)
2) C Projects (Book w/CD) (excellent introduction to actual "Project level" Programming with C i.e you know the language but need to see the way to build up code to do something useful)
3) Unix Shell Programming (Book) (excellent shell scripting book with last chapter creating a DBMS using simple shell functions)
4) Writing TSRs through C. Excellent introduction to hardware/OS/interrupt level programming using C.