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,
- & address operator
- * de-referencing operator
- -> 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");
}
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: AThe 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