// More on pointers and functions

#include <stdio.h>


typedef struct test{
     int i1;
     int i2;
}Test;


void exchange (int * const pint1, int * const pint2);
void exchange2 (int int1, int int2);
Test exchange3(Test t);
void exchange4(Test *t);

int main (void)
{
 
     int   i1 = -5, i2 = 66, *p1 = &i1, *p2 = &i2;
     Test t = {1,2};
     Test t2;
     Test t3 = {3,4};
     Test *test = &t3;

     printf ("i1 = %i, i2 = %i\n", i1, i2);

     /*p1 and p2 are pointers, so the exchange of values will work as hoped*/
     exchange (p1, p2);
     printf ("i1 = %i, i2 = %i\n", i1, i2);

     /*By placing the & in front of i1 and i2 you are saying I am going to 
       use these as pointers.  This implicitly makes i1 and i2 pointers*/

     exchange (&i1, &i2);
     printf ("i1 = %i, i2 = %i\n", i1, i2);

     /*When you pass the values of i1 and i2 to the function they are only 
      * copies of i1 and i2  You need to use pointers.  Or if you could return
      * both values then it would be fine, but in this case you can not. */
     exchange2 (i1, i2);

     printf ("i1 = %i, i2 = %i\n", i1, i2);

     /*We can pass in a structure and return the changed struct.  But this does
     not change the original structure.  Again you are only making a copy of the
     structure that you passed in.*/
     t2 = exchange3(t);

     printf ("t2.i1 = %i, t2.i2 = %i\n", t2.i1, t2.i2);
     printf ("t.i1 = %i, t.i2 = %i\n", t.i1, t.i2);


     printf ("Before Exchange: t3.i1 = %i, t3.i2 = %i\n", t3.i1, t3.i2);

     exchange4(test);

     printf ("After Exchange: t3.i1 = %i, t3.i2 = %i\n", t3.i1, t3.i2);
     printf ("test->i1 = %i, test->i2 = %i\n", test->i1, test->i2);

     return 0;
}


/*This function will work correctly and exchange the values because we are
 *using pointers.  Notice:  *pint1 = *pint2.  Why is this OK.  Didn't we just 
 *learn that a *const ptr must always point to the same address.  Yes, but 
 *here we are using indirection (dereferencing) to change the value of what 
 *pint1 is pointing to not where it is pointing.  */
void  exchange (int * const pint1, int * const pint2)
{
     int  temp;

     temp = *pint1;
     *pint1 = *pint2;
     *pint2 = temp;
}


/*The next two functions will not work as you expect.  Why????  Because 
 *remember when we pass a variable it is copied in memory and what ever 
 *happens to that variable within the function stays within the funtion.  
 *This is called scope.  If we passed a pointer then what happens to a 
 *pointer within a function DOES NOT STAY within the function.  Think 
 *about it when the function makes a copy of the pointer.  It is still 
 *copying an address so when it makes a change to whatever the pointer 
 *is pointing to it is it is a permanent change.  */
void exchange2(int int1, int int2)
{
     int temp;

     temp = int1;
     int1 = int2;
     int2 = temp;
}


Test exchange3(Test t)
{
     int temp;
     temp  = t.i1;
     t.i1 = t.i2;
     t.i2 = temp;

     return t;
}

/*This will work as you expect because the are passing pointer. */
void exchange4(Test *t)
{
     int temp;
     temp  = t->i1;
     t->i1 = t->i2;
     t->i2 = temp;

}
