Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Thursday, 23 January 2014

how the pointer variables can be directly used in expressions



The program in below 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 arithmetic.

ILLUSTRATION OF POINTER EXPRESSIONS
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 = %u\n", p2);                          
       printf("\n");                                               
       printf("a = %d, b = %d\n", a, b);                           
       printf("x = %d, y = %d\n", x, y);                            
       *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