Friday, August 17, 2012

Pointer

A pointer is a variable which contain the memory address. It can also points to a specific data types (like struct). Three operator are commonly  used  when dealing with pointer are,
  1. &  address operator
  2. *  de-referencing operator
  3. -> structure pointer operator

Example:
In this example you will see how pointer works. 


In the above figure you see that c is a variable of char data-type and it has value "A" inside. The address of variable c is 0X3000


Now in this figure you see that c is a variable of char data-type and it has value 'A' inside. The address variable c is 0X3000. And a pointer variable cptr of char data-type. The cptr pointer variable having the address of c variable rather value. So this is one the important difference between a general variable and pointer variable is that a pointer variable contains memory address.

A Programatic example

#include <stdio.h>
void main()
{
char c ='A';
char *cptr;
cptr=&c;
printf("\n Value of c is: %c",c);
printf("\n The address of &c is: %p",&c);
printf("\n The address of &cptr is: %p",&cptr);
printf("\n Value of cptr is: %p",cptr);
printf("\n Access variable which *cptr point to is: %c",*cptr);
printf("\n-----------------------------------------------------\n");
}


----------------------------------------------------------------------
Output
----------------------------------------------------------------------
Value of c is: A
The address of &c is: 0xbfe8447f
The address of &cptr is: 0xbfe84478
Value of cptr is: 0xbfe8447f
Access variable which *cptr point to is: A
----------------------------------------------------------------------

 










No comments:

Post a Comment