Friday, February 6, 2015

C Pointers in a Simple English

Everyday Analogy

What is a pointer?

A pointer is a 'piece of paper' which stores the 'phone no' of a 'target' object .
Using the pointer we can access the target.
Since we can easily change the target, we can create programs which don't need to know what variable/object they're operating on. They just operate on the variable indirectly through the pointer and don't care what the target is

Variables are like trees, they're rooted to one place
Pointers are like cheetahs, they're agile and dangerous.

 Imagine a table with rows and columns, let's say this represents a "logical RAM".
 Each cell can store data. So each cell can act as a variable.
Some of these variables could store locations of other cells instead of just number values.

So the cells are numbered left-to-right and top-to-bottom from 1-12 in the figure below:

So the 3rd cell stores the value 150 in it. And the 11th cell stores the value 200 in it.
So the 7th cell stores the address '3' to point to the 3rd memory cell (containing the value 150 in it).
And the the 10th cell stores the address '11' to point to the 11th memory cell (containing the value 200 in it).

Everyday Analogies:

A) Telephone Number Analogy :

We regularly use pointers in everyday life when we use telephone numbers.
 [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.

Ghost-to-Ghost Hookup (Find a Person via your Friend Network):

Let us say Tom wants to tell his friend something important.
He can either physically go and talk to him or he can phone him.

Lets assume Tom phones Jerry.
   Tom ---> Jerry

Tom doesn't have Jerry's no. Tom calls up 1 to get Jerry's no.
   Tom ---> 1 ---> Jerry

Tom follows the "Six Degrees of Separation Rule" as he knows that someone in a path going outward will know Jerry's no.
   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.

B) Links of a Chain : 

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 Mickey is a better source of info and gives Mickey's number instead.

 a)Original path:
   Tom ---> 1 ---> 2 --->...N ---> Jerry

   Tom ---> 1 ---> 2 --->...N ---> Mortimer

We can remove a single link from anywhere in the chain and re-create the (slightly smaller) chain by reattaching the links near the break in the chain. There is a minimal cost of inserting new links or removing existing links in the chain. This is exactly the property which allows efficient linked lists and other data-structures to be used without being stuck with unwieldy arrays.
It takes a lot of copying to cover up the "hole" in an array if we need to delete or insert at the front or middle.
But pointers just require reassigning the links fix the "break" in the chain.

C) Train Shunting :

This is similar to shifting tracks for a train, we can easily divert the train to whatever track by using the shunting mechanism.

D) Postal Address :

Though not as precise and neat as the telephone number analogy you can think of a pointer variable as an envelope with the address of a person on a paper inside.

When do we use Pointers?

 a)When we want to randomly point to and access different areas of memory (say a sequence of objects).
 b) Access a specific memory location. (accessing a port or a vdu buffer)
 c) Create an anonymous object.
 d) Save our position in a sequence.
 e) Avoid needless copying/moving of data items in a data-structure by just adjusting few pointers.

A pointer can point somewhere (an object) or anywhere (an address).
 It can be repositioned to point elsewhere.

 Access the object contents by location (Like voodoo)
 Insulate knowledge of identity of target object. (Bearer Cheque, Lottery winner)
 Anonymous objects (using new or malloc )
 Iterating over a Sequence

  ptrvar = &Object1;    //targeting the pointer
  ptrvar           Object1
 [ 1000 ] -----> [........] location 1000

  ptrvar =  &Object2;    //retargeting the pointer
 [ 2000 ] -----> [........] location 2000

We need pointers to keep track of our position as we access different locations.
Everyday usage :
The index finger 'points' to our current location in a phone-directory as we rapidly scan through the entries.
Website URLs, Postbox numbers or Postal addresses.

 We want to call up all people with the prefix 2375XXXX we just start at 23750000 and then
 keep adding 1 to the no. till we reach 23759999
 We could call up all people with 2375XXXY where Y is an even no. and so on.
 We can randomly dial any no. without knowing the person at the other end (anonymous object)

How do we use Pointers?

a) Example code to understand Pointers : C Source Code/Pointers 

b) C Code to traverse and insert a node in a linked-list:
 typedef struct list {
   int data;
   struct list *next;

 void printlist(List *first)
  List *tmp;
  for(tmp = first; tmp != NULL ; tmp = tmp->next ) { // step through the sequence
      printf("%d", tmp->data);
  return ; 

 int main()
  int x=1,y=2,z=0;
  int num[3] = { 1, 2, 0 };
  List *head = makelist();    //returns an anonymous list of objects ->[10]->[20]->[30]->null
  add(&x,&y,&z);            // z = 3
  add(&num[0],&num[1],&num[2]);        // num[ 1, 2, 3]

  return 0;

 void add(int *a,int *b,int *c)
  return *c = *a + *b;

Location access using Pointer or Array Notation:

int iarr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //Note : arrays are 0-indexed in C
int * iptr = &iarr[0];   //store address of 1st location of array in iptr
*iptr = 42;  //get the address from envelope using star operator, go to location and assign '42' number

iptr[0] = iptr[9];  // read last integer of array and assign to 1st cell of array (using array notation)
*(iptr+0) = *(iptr+9);  //Same as previous code we're just using pointer notation (instead of array notation).

See Also : 
  1. The 5 Minute Guide to C Pointers :
  2. "Understanding Pointers in C" by Yeshawant Kanetkar (BPB Publications).
  3. "Understanding C by Learning Assembly" :
  4. Learning C with GDB :
  5. C Pointer Tutorial : 
  6. Super Tutorial on Linux Memory Management :
  7. Internals of C++ and Unix :

Tuesday, August 26, 2014

Installing and Testing out your own crowd-sourced App with PyBossa

Testing PyBossa with a Virtual Machine :

Install Virtualbox and Vagrant first.

Problem : The 'vagrant up' fails on WinXP while installing 'hashicorp/precise32' dependency.
Following error message is seen : 

Cause :  This is caused by missing or failure in running bsdtar.exe.
For More Details see :
1) Install Mingw with bsdtar.exe
2) "However, installing MinGW for the bsdtar.exe, brought up another issue.
bsdtar.exe wouldn't run because it needed 'msys-lzma-1.dll' but 'msys-lzma-5.dll' was available.
Just copied and renamed to expected filename and it worked!!"

Now precise32 installs and 'vagrant up' succeeds.

Next step

Problem : Running the 'source' script fails
Cause : bash requires usage of ./scriptname to execute script in current directory.
Solution : Just changing the docs line 'source' to 'source ./' fixes the problem.

Problem : 'source ./' still fails
Cause : It gives errors for trailing ^M characters in the script, DOS line-feed chars.
Solution : Editing the file and removing trailing ^M characters solves the problem.

Use your web browser to browse Pybossa welcome page with this URL 'http://localhost:5000'

Thursday, July 17, 2014

test discourse

1) Configure Discourse Server Settings as described here:

2) The Discourse comments should appear here ===>

Friday, July 4, 2014

TED Video : Arduino Make the World Your Playground with Do-It-Yourself Intelligent Toys

+ Best Quote by Massimo Banzi about Arduino community - "Going Into EVERY FIELD I can IMAGINE"

+ Massimo Banzio 10 Part Video Tutorial :

+ For Artists, Designers and Hobbyists - Play with a Sensing Computer using Arduino :

+ MAKE magazine's YouTube Channel :

+ Share/Teach what you make :

+ Students can upload their experiments to Arduino based Lab to conduct experiments in SPACE!! :

+ Intel Galileo = Arduino + Linux + Memory + Sensors + Ports  :

Tuesday, July 1, 2014

Test Google Transliterate Widget

Type in Indian languages (Press Ctrl+g to toggle between English and Hindi)
Type in