Demonstrating Pointer through an example :
# include < stdio.h >
main ( )
{
int a;
int *p;
printf(" \n Enter the value for a " ) ;
scanf ( "%d", &a );
p=&a;
printf ( " \n a = %d ", a );
printf ( " \n Address of a is %u ", &a );
printf ( " \n using pointer a is %d ", *p );
printf ( " \n Address of a using pointer is %u ", p );
printf ( " \n Pointer p address is %u " , &p );
}
In the above program variable a is declared and assume that its value is 10. A pointer variable is also declared and holds the address of variable a (i.e. 1001). The output will be as follows,
a = 10 /* because a value is 10 */
Address of a is 1001 /* &a means address of a */
using pointer a is 10 /* *p => *(1001) => contents of 1001 is 10 */
Address of a using pointer is 1001 /* p contains 1001 i.e. equal to output of &a */
Pointer p address is 1008 /* &p means address of p */
# include < stdio.h >
main ( )
{
int a;
int *p;
printf(" \n Enter the value for a " ) ;
scanf ( "%d", &a );
p=&a;
printf ( " \n a = %d ", a );
printf ( " \n Address of a is %u ", &a );
printf ( " \n using pointer a is %d ", *p );
printf ( " \n Address of a using pointer is %u ", p );
printf ( " \n Pointer p address is %u " , &p );
}
Diagram : Diagrammatic representation of pointer
a = 10 /* because a value is 10 */
Address of a is 1001 /* &a means address of a */
using pointer a is 10 /* *p => *(1001) => contents of 1001 is 10 */
Address of a using pointer is 1001 /* p contains 1001 i.e. equal to output of &a */
Pointer p address is 1008 /* &p means address of p */
Comments
Post a Comment