Evaluation of pointer expressions

Program 52) Write a program to illustrate the use of pointers in airthmetic operations.
This program shows how the pointer variables can be directly used in expressions. It also illustrates the order of evaluation of expressions. For example, the expression
                       
                          4* - *p2 / *p1 + 10
is evaluated as follows:
                        ( (4* (-(*p2))) / (*p1)) + 10

When *p1 = 12 and *p2 = 4, this expression evaluates to 9. Remember, since all the variables are of type int, the entire evaluation is carried out using the integer airthmetic.

                                  PROGRAM

main ( )
 {
     int a, b, *p1, *p2, x, y, z ;
     a = 12 ;
     b =  4 ;
     p1 = &a ;
     p2 = &b ;
     x = *p1 *  *p2 - 6 ;
     y = 4* - *p2 / *p1 + 10 ;
     printf ("Address of a = %u\n", p1) ;
     printf ("Address of b = %d\n",p2) ;
     *p2 = *p2 + 3 ;
     *p1 = *p2 - 5 ;
     z    =  *p1 *  *p2 - 6 ;
    printf ( "\na = %d, b = %d,", a, b) ;
    printf (" z = %d\n", z) ;
 }

Output:        Address of a = 4020
                     Address of b = 4016
                     a = 12, b = 4
                     x = 42, y = 9
                     a = 2, b = 7, z = 8 

  

No comments:

Post a Comment