[圖解C] Example 807: Call by Address & Call by Value

// Example 807: Call by Address & Call by Value

#include <stdio.h>
#include <stdlib.h>

// declare pointers as parameters: int *
void test_func(int *, int *, int, int);

int main()
{
    int a=11, b=22, c=30, d=40;
 
    printf("in main, Before: A =%d , B =%d , C =%d , D =%d\n"
           , a, b, c, d);
 
    // a, b >> call by address >> using "&"
    // c, d >> call by value
    test_func(&a, &b, c, d);
 
    // call by address >> change the original data
    // call by value >> operating on copy, don't change original data
    printf("in main, After: A'=%d , B'=%d , C'=%d , D'=%d\n"
           , a, b, c, d);
 
    system("pause");
    return 0;
}


void test_func(int *x, int *y, int v1, int v2)
{
     int temp1, temp2;
   
     printf("in subfunc, Before: X =%d , Y =%d , V =%d , W = %d\n"
            , *x, *y, v1, v2);
   
     temp1 = *x;
     *x = *y;
     *y = temp1;
   
     temp2 = v1;
     v1 = v2;
     v2 = temp2;
   
     printf("in subfunc, After: X'=%d , Y'=%d , V =%d , W = %d\n"
            , *x, *y, v1, v2);
}