Previous      Next

//            Pointers - another approach                                        
//                                                                               
// Declaration: * means "pointer variable"                                     
//     Thus "int *b;" declares b to be a variable of type pointer to int       
// Non-declaration: * means contents of, & means address of                      
//     Thus "*b = 10;" means the int location pointed to by b gets value 10    
//     "b = &a;" means the contents of b is set to the address of variable a

void swap (int *x, int *y)
{
   int t;
   
   t = *x;
   *x = *y;
   *y = t;
}

void main()
{
   int a = 10, b = 20;
   int *ap = &a, *bp = &b;
   
   printf("\nValue of a: %d  Value of b: %d",a,b);
   swap (ap, bp);
   printf("\nValue of a: %d  Value of b: %d\n",a,b);   
}

// Sample Output:
//   Value of a: 10  Value of b: 20
//   Value of a: 20  Value of b: 10