Short-hand operators
 

  • C has many operators that we call short-hand operators

  • Short-hand operators simplifies an expression written using "non-short-hand" operators

    Example:

        Expression   Expression with a short-hand operator
        ==========   ========================================
        i = i + 1;   i++;    (combines = and + into 1 operator) 
        i = i * 7;   i *= 7; (combines = and * into 1 operator)
      

The pointer-member-access operator ->

 #include <stdio.h>     

 struct BankAccount {   
   int   ID;            
   float balance;
 };

 int main( int argc, char *argv[] )
 {
   struct BankAccount john, mary, *p ;

   john.balance = 500;
   p = &john;                    // Now *p ≡ john
   (*p).balance = (*p).balance + 2000; 

   mary.balance = 500;
   p = &mary;                    // Now *p ≡ mary
   (*p).balance = (*p).balance + 9000; 

   printf("j: %f  m:%f\n\n", john.balance, mary.balance); 
 }    

The pointer-member-access operator ->

 #include <stdio.h>  // Operator ->: p->x ≡ (*p).x 

 struct BankAccount {   
   int   ID;            
   float balance;
 };

 int main( int argc, char *argv[] )
 {
   struct BankAccount john, mary, *p ;

   john.balance = 500;
   p = &john;                    // Now *p ≡ john
   (*p).balance = (*p).balance + 2000; 

   mary.balance = 500;
   p = &mary;                    // Now *p ≡ mary
   (*p).balance = (*p).balance + 9000; 

   printf("j: %f  m:%f\n\n", john.balance, mary.balance); 
 }    

The pointer-member-access operator ->

 #include <stdio.h>    // Program re-written using ->  

 struct BankAccount {   
   int   ID;            
   float balance;
 };

 int main( int argc, char *argv[] )
 {
   struct BankAccount john, mary, *p ;

   john.balance = 500;
   p = &john;                    // Now *p ≡ john
   p->balance = p->balance + 2000; 

   mary.balance = 500;
   p = &mary;                    // Now *p ≡ mary
   p->balance = p->balance + 9000; 

   printf("j: %f  m:%f\n\n", john.balance, mary.balance); 
 }