The relationship between formal parameters and actual parameters in C language is: the formal parameter is a copy of the value of the actual parameter. Regardless of the type of the parameter, the formal parameters passed to the subfunction are only the values of the actual parameters. Therefore, when changing the value of the formal parameter variable itself, it has nothing to do with the actual parameters. In other words, changes to the formal parameters will not affect the actual parameters.
However, if the parameter type is a pointer type, you can change the data in the actual parameter address by modifying the data in the address pointed to by the pointer variable.
Therefore, when using the SWAP function to exchange data, two points must be guaranteed:
The parameter must be a pointer type, pointing to the data to be exchanged
When exchanging, the data in the address must be exchanged, not the address itself.
Reference code:
//Wrong exchange code 1
voidswap1(inta,intb)//The parameter is not a pointer to the data to be exchanged
{
intt=a;
a=b;
b= t;
}
//Wrong exchange code 2
voidswap2(int*a,int*b)
{
int*t=a;//Exchange the formal parameter variable itself
a=b;
b=t;
}
//Correct exchange code
voidswap(int*a,int*b)
{
intt=*a;//Exchange the data in the formal parameter variable address, Pointer variables remain unchanged
*a=*b;
*b=t;
}
The poster needs to have a deeper understanding of the parameters and return values of the function~
First of all The return value of a function must be only one variable. The return(a,b) in your swap function may not pass the compilation~
At the same time, the parameters of the function can be passed in three forms: address, value, and reference. In your case, it is recommended to use the pointer method to directly modify the values of a and b in swap. The code is as follows:
void swap(int*a, int*b)
{
int t;
t=*a;
*a=*b;
*b= t;
}
The method called in main is swap(&a,&b);
The above is the detailed content of Which exchanges (currency exchanges) are CSWAP coins listed on?. For more information, please follow other related articles on the PHP Chinese website!