Thursday 28 April 2011

Pointers in C


Pointer variable is a variable that holds the memory address of a some other variable.

int var = 10;


Here we have a variable "var" whose data type is of integer and the value it holds is 10. It means whenever we will use "var" in our program then we are using 10. This "var" is for human convince. An electronic machine understands only 0 & 1, means binary. So, whenever computer will look for "var" it will look for its corresponding address in memory and it is a number. Pointer is way to play with that memory address because it points to that memory address.

& and * are two operators used in pointer operations.

& means "address of"

Till now you have used it many times.

Code:
int main (void) {

int var;
printf("Input an interger -> ");
scanf("%d",&var);
printf("Value of interger -> %d\n",var);

return 0;

}
Here in statement scanf("%d",&var); scanf() will take the input from command line and it will store that value at the address of "var".
Suppose starting memory address of var is 0xbfd025f0. After input it will store the value supplied by user in memory block which starts from 0xbfd025f0

* means "pointer"

int *varPtr;
*varPtr means this variable is going to hold address of some variable. How ? See next line.
varPtr = &var;

When unary operator (&) is associated with a variable then it give the address of that variable. In above statement the address of variable "var" is assigned to the pointer variable "varPtr". That means now value held by varPtr is 0xbfd025f0, which we supposed above.

varPtr                                var                                    Variable Name  
 ++++++++++++++      ++++++++++++++
+   0xbfd025f0     +      +       10              +            Value Held
++++++++++++++      ++++++++++++++
 0xa0302cd0                0xbfd025f0                        Memory Address of Variable

varPtr is also a variable so, it too has a memory address. When an unary operator (*) is associated with variable "varPtr" like *varPtr, it means now it is pointing to the value which held by varPtr (0xbfd025f0), ultimately which a memory address.

It is like friend of a friend. You have some work with a person A and yon have no direct contact with him. But that person A is friend of one of your friend Z. Now what you are going to do, you are going to call your friend Z and from there you will get contact of A.

Code:
int main (void) {

int var = 10;
int *varPtr;
varPtr = &var;   // assign the memory address of var to varPtr

printf("Value of var -> %d\n",var);
printf("Value of var -> %d\n",*varPtr);

return 0;

}

OUTPUT
Value of var -> 10
Value of var -> 10

Both printf() will give you 10 as output. In 1st printf() we are directly printing the value of "var" and in 2nd printf() we are doing it indirectly, means by using pointer.

I hope now a beginner has some idea of pointers. Same way functions too have memory address and using that memory address we can call them.
void fun (void)  {

--------do something--------
}

fun() occupy some memory block and &fun will give you the starting value of that memory block.

+++++++++++++++++++++++++++++++Sample Program+++++++++++++++++++++++++++++++
#include

void msg (void) {
   printf("Inside msg function\n");
}

int calSum(int x, int y) {

   printf("Value of x -> %d\n",x);
   printf("Value of y -> %d\n",y);

   return (x+y);

}

int main (void) {

   int var, sum;
   var = 10;

   int *varPtr;         //varPrt is pointer to integer variable
   void (*msgPtr)(void);   //msgPtr is pointer to a function whose return type is  void [ void msg (void ) ] and arguments passed are void [ void msg (void) ]
   int (*sumPtr)(int,int);  //sumPtr is pointer to a function whose return type is integer [ int calSum (int ,int) ]and two integer arguments passed [ int calSum (int, int) ]

   varPtr = &var;         // assigne address of var to varPtr
   msgPtr = &msg;         // assigne address of msg to msgPtr
   sumPtr = &calSum;         // assigne address of calSum to sumPtr

   printf("Value of var -> %d\n".*varPtr);      // print value of var using pointer

   (*msg)();         // calling funtion msg() using pointer

   sum = (*sumPtr)(5,4);      // calling function calSum(int,int) using pointer and storing return value in variable sum

   printf("Sum of two numbers -> %d\n",sum);   //print value of sum

   return 0;
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
OUTPUT
Value of var -> 10
Inside msg function
Value of x -> 4
Value of y -> 5
Sum of two numbers -> 9

P.S.
For beginner i tired to explain it in most simple and layman language. I took example of only integer data type for simplicity and tried to explain pointer to function also because i felt that for a beginner it better to understand them from starting. And this not the end of pointers.

No comments:

Post a Comment