However, if the passed parameter is an array name and the subscript is used in the function to reference the parameter of the array, in this case, the number of elements in the array is actually modified by the array elements in the calling program. The function will access the array elements of the calling program, and the array will not be copied. This behavior is called "Address Transfer call ".
The behavior of array parameters seems to be in conflict with the calling rule for value passing. However, there is no conflict here: the value of the array name is actually a pointer, and a copy of the pointer is passed to the function. Subscript reference is actually another form of indirect access. It can perform indirect access operations on the pointer and access the memory location pointed to by the pointer. The parameter is actually a copy, however, the original array is accessed by indirect access operations on this copy.
The so-called address transfer call is to pass a pointer to the required element, and then execute indirect access operations on the pointer in the function to access the data. The array name as a parameter is a pointer, And the subscript reference actually executes indirect access.
In this case, what is the behavior of calling the array value transfer? A copy of the parameter is passed to the function (a copy of the pointer pointing to the starting position of the array), so the function can operate its pointer parameter freely, you don't have to worry about modifying the corresponding pointer as a real parameter.
Therefore, all parameters are passed by passing values.
A classic function that exchanges two integers (no effect) can be called by explicitly passing values.
void swap(int x,int y){ int temp; temp = x; x = y; y = temp; }
This is an integer exchange with no effect: because it is actually a copy of the parameter, the original parameter value is not exchanged.
If you want to modify the above program, you can use a pointer to implement it:
void swap(int *x,int *y){ int temp; temp=*x; *x=*y; *y=temp;}
To access the value of the calling program, we must pass the pointer to the variable we want to modify to the function. Then, the function must use an indirect access operation on the pointer to modify the variable to be modified.
In the main function, because the function expects to accept a pointer, we should call it in the following way: swap (& a, & B );