Pointer is a variable that contains address of another variable. Now this variable itself might be another pointer. Thus, we now have a pointerthat contains another pointer’s address.
Example :
#include <stdio.h>
#include <conio.h>
void main()
{
int i = 3, *j, **k ;
j = &i ;
k = &j ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of i = %u", *k ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nAddress of j = %u", k ) ;
printf ( "\nAddress of k = %u", &k ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of k = %u", k ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", * ( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
printf ( "\nValue of i = %d", **k ) ;
}
Output :
Address of i = 65524 Address of i = 65524 Address of i = 65524 Address of j = 65522 Address of j = 65522 Address of k = 65520 Value of j = 65524 Value of k = 65522
Explanation :
Here the declarations as follows :
int i = 3; //i is an ordinary int,
int *j; //j is a pointer to an int
int **k; //k is a pointer to an integer pointer
Now initializations as follow :
j = &i ; //address of variable i is stored in j
k = &j ;//address of variable j is stored in k
printf ("\nAddress of k = %u", &k); will return address of k.
printf ("\nAddress of j = %u", k); will return value of k or address of j.
printf ("\nAddress of i = %u", *k); will return the address of i.
*k = *(&j)=>*(65522) will return value stored at this address of the variable j i.e 65524 = 65524
printf ("\nValue of i = %d", **k); will return the value of i.
**k = **(&j)=>[*(&j)] will give the value stored at the address of variable j i.e 65524 which is the address of variable i i.e &i.
= *(&i) will give value stored at the address of variable i i.e 3.
No comments:
Post a Comment