1. in C language, all data parameters in non-array form are called in the form of value transfer. In the process of value transfer, parameters in the form of called functions are regarded as local variables of the function, that is, opening up space in the memory stack to store the values of real parameters put in by the main function, this becomes a copy of the real parameter. Therefore, the called function cannot modify the value of the actual variable as the real parameter, but can only modify the backup that is passed to it.
2. In the address transfer process, although the parameter of the called function is also used as a local variable, the memory space is opened up in the stack, however, the address of the real parameter variable put in by the main function is stored, and any operations of the called function on the form parameter are processed as indirect addressing, that is, access the real variable in the main function through the address stored in the stack. Because of this, any operation performed by the called function on the form parameter affects the real parameter variables in the main function.
3. At the same time, there is another issue worth noting: An array cannot be passed to a function.
Verify that:
Void fun (char a [10]) {int I = sizeof (a); char c = A [3];}
If the array is actually passed into the function, the value of I should be 10. Unfortunately, the value of I is 4, and it is not passed.
In C, when a one-dimensional array is used as a function parameter, the compiler always parses it into a pointer pointing to its first element address.
For the above value transfer and address transfer, the following usesCodeNotes:
/** Main. c *** created on: Apr 28,201 2 * Author: Root */# include <stdio. h> void swap_val (int x, int y) {int temp; temp = x; X = y; y = temp; printf ("x = % d, y = % d \ n ", x, y);} void swap_addr (int * X, int * Y) {int temp; temp = * X; * x = * Y; * Y = temp;} int main (void) {int A, B; printf ("Please input two integer \ n"); scanf ("% d ", & A, & B); if (a <B) {swap_val (a, B); printf ("A = % d, B = % d \ n",, b); swap_addr (& A, & B); printf ("A = % d, B = % d \ n", a, B);} return 0 ;}
The verification result is as follows:
Please input two integer
1
2
X = 2, y = 1
A = 1, B = 2
A = 2, B = 1