Accessing a variable through its pointer

Program 51) Write a program to illustrate the use of indirection operator '*' to access the value p[ointed to by a printer.
The program clearly shows how weanm access the value of a variable using a pointer. You may notice that the value of the pointer is 4104 and the value its points to is 10. Further, you may also note the following  equivalness:
                 x = * (&x) = * ptr = y
                 &x = & *ptr

                                        PROGRAM

main ( )
 {
      int x, y ;
      int *ptr ;
      x = 10 ;
      ptr = &x ;
     y = * ptr ;

     printf ("Vlue of x is %d\n\n", x) ;
     printf ("%d is stored at addr %u\n", x, &x) ;
     printf ("%d is stored at addr %u\n", *&x, &x) ;
     printf ("%d is stored at addr %u\n", *ptr, ptr) ;
     printf ("%d is stored at addr %u\n", ptr, &ptr) ;
     printf ("%d is stored at addr %u\n", y, &y) ;
     *ptr = 25 ;
     printf ("\nNOW x = %d\n", x) ;
 }

Output:            Value of x is 10
                         10        is stored at addr 4104
                         10        is stored at addr 4104
                         10        is stored at addr 4104
                         4104    is stored at addr 4106
                         10        is stored at addr 4108
                         Now x = 25

 

No comments:

Post a Comment