C language

POINTERS Illustration

The pointer, as the name suggests, is something which is pointing to or directing to something. The pointer is used to store the address of another variable and the data type of the pointer is same as data type of variable it is pointing to. As the variable which points to other variable is called pointer and the variable which is pointed to by the pointer is called pointee. We represent the relation between the two as:

www.exploreroots.com

We can also have pointer to pointer and we declare it as:

Int **z;

z=&y;

And the memory map can be represented as:

www.exploreroots.com
int* x;
int* y;
int i;
int j;

Memory map at this moment is as:

www.exploreroots.com

x=&i; // the statement assigns the pointee i to pointer x or we can say address of i is stored in variable x.

www.exploreroots.com

*x=2; // the pointee of x gets the value 2. This statement is equivalent to i=2;

Memory map at this moment is as:

www.exploreroots.com

*y=4// ERROR as no pointee is assigned

Remember that using pointers, we need to do two tasks: one creating a pointer and other as assigning a pointee to pointer. In the above example one task was done and hence the ERROR.

y=x; // this statement assigns the pointee of x to y also. Hence pointee of both x and y are same.

Memory map at this moment is as:

www.exploreroots.com

*y=4; //As pointee for both x and y are same. Hence changing the value of one would change the same for other.

Memory map at this moment is as:

www.exploreroots.com

y=&j;// Now pointer y is assigned a new pointee j.

Memory map at this moment is as:

www.exploreroots.com

Hence now both pointers points to different memory locations.

*y=8;

 Memory map at this moment is as:

www.exploreroots.com

*y=x; // gives an ERROR as data types don’t match. x is not an int but pointer to int

x=*y; // gives ERROR again as *y is not Int* but int. In other words *y is not a pointer but a Int value while x is int* i.e. pointer to int.

*y=i; // both the data types are same INT and hence value of i is assigned to pointee of y.

i=7; // value of i is changed i.e. pointee of x is changed

Memory map at this moment showing result of above 2 statements is as:

www.exploreroots.com

x=y; // both x& y are int* i.e. pointers. Now both pointers point to same place and previous pointee of x retains the value.

Memory map at this moment is as:www.exploreroots.com

3 Replies to “POINTERS Illustration

  1. Pingback: namo333

Leave a Reply

Your email address will not be published. Required fields are marked *