C language function pointer in the parameter passing

int add (int a, int b) // function parameter passing when using the int integer data, the value type itself. When the function is actually invoked, the argument own copy, and the copy is transmitted to the parameter is operated. Argument is not involved in their actual operations. Therefore, in this function, the argument itself is not changed.

int main ()

{

  int x = 3, y = 5;

       swap(x, y);

  printf ( "x =% d, y =% d \ n", x, y); // exchange failure

       swap_pointer(&x, &y);

  printf ( "x =% d, y =% d \ n", x, y); // successful exchange

}

int swap (int a, int b) // C language, the function call, the parameter argument to actually call-by. That is, the arguments x and y values ​​a copy of their pass and a parameter B; to give the actual exchange is a swap function and B, instead of arguments x and y, thus executing the function after the values ​​of x and y has not changed.

{

  int temp;

  temp = a; // a parameter inside the swap is obtained when the actual call is a copy of the argument x, the value of x is equal to and just only, not associated with any other, thus here not have access to arguments x

       a = b;

  b = temp;

  return 0;

}

When int swap_pointer (int * p1, int * p2) // C language function calls, has been the call-by. It has been copied argument that is actually transmitted, but the formal and actual parameters are not present in the function of x and y, but x and y address values. In this case, let us call a function in access to external function arguments by way of indirect access * p. // (if you do not use the pointer, and then call the internal functions can only be accessed parameter can not be accessed outside the function arguments)

{

  int temp;

  temp = * p1; // when the actual call, p1 get the argument is the address of x & x, therefore x * p1 is represented by

       *p2 = *p1;

       *p1 = temp;

       return 0;

}

Guess you like

Origin www.cnblogs.com/jiangtongxue/p/11079651.html