Monday, 17 June 2013

Pointers In C

Pointers are very useful while programming in C and they are easy to learn.
We start discussion with what are pointers and why they are used.

When you create a variable in C, some memory is allocated for it from primary memory (RAM) and we can refer to that memory location by the name of the variable.
For example consider int var; . Now a memory location will be allocated to 'var' and we will refer to that location using variable name var.
Now assume that the memory is addressed using sequential numbers, and var is allocated at location 1000. which means if we do var = 10; value 10 will be stored at memory location 1000 and 1001 as int is of 2 bytes.

1000 -> 0000 1010 (10)
1001 -> 0000 0000 (00)

Thus we can say that a variable is a memory location allocated during execution and its value is value stored in corrosponding location.
We can obtain address of any variable using '&' operator, so in our example &var will be 1000.

Now different type of variables store different type of values like integer digit, floating point data, character data etc. The vary same way pointer are also variables and they store the address of other variables.
They are declared same way normal variable but with an extra * before their name.
For example int *ptr; . This statement will declare a variable of int* type which means it is a pointer and it will contain the address of int variable.

There are two ways of assigning a value (address of a variable) to the pointer.
  1. At declaration time :
    int *ptr = &var;
    Here ptr is declared as a pointer to int and address of var is stored in ptr.
  2. After declaration :
    int *ptr; ptr = &var;
    Here we first declare the pointer and then assign the value.

Note that we can not assign value to the pointer directly like ptr = 1000; or int *ptr = 1000; . Only assignment through & operator or malloc/calloc/reaclloc is allowed with exception being 0 and null i.e. ptr = 0; or ptr = null is allowed. Also note that pointer is also a variable and its value is also stored at some memory location.
Consider following figure.
Here we can see that var is allocated memory location 1000 and its value is 1234 and ptr is allocated memory location 2000 and its value is 1000 which is address of var. Thus value of ptr and &var is same as &var gives address of var and so does ptr.
Now if we want to access any variable pointer by a pointer we use dereference operator or indirection operator which is * in C. So writing *ptr will yield the same value as the var.
Thus we can use pointer to access the address of a variable and also the value. Also note that a int* type pointer can only point to a int variable but a void* pointer also called generic poiner can point to variable of any datatype.

Pointer To Pointer:

A pointer can be used to store address of a variable like int, float, char, it can also point to another pointer i.e. store address of another pointer, such pointers are called pointer to pointer.
Pointer to pointer can be declared using data-type **ptrptr; Consider following fragment of the code.
int i=18; int *ptr = &i; int **ptrptr = &p;

Here ptrptr is a pointer to an int pointer ptr and ptr is a pointer to an int. Following figure illustrates the above scenario.

As we can see here we can also use ptrtpr to get value stored in i.

The * operator is evaluated as following. Suppose we have ***x then it is evaluated as *(*(*x)). So we can evaluate it as first get the value stored as address x say y, so expression becomes *(*y). Now again get the value stored at address y say z, so expression is now *z which is value stored at address z.

For our case **ptrptr means get the value stored at address 2000 which is 1000 and then get the value stored at 1000 which is 18.

Summary :
A variable is a memory location with a name given to it and it declared like data-type i;
A pointer variable is declared as data-type *ptr; and it points to a variable declared with same data-type.
A pointer can be assigned value using & operator.
Value of a variable pointed by a pointer is retrieved using * operator.
A Pointer to pointer is a pointer that points to a pointer that is it stores the address of a pointer.
Ads :
Buy Sony, Canon, JVC Cameras on
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay |