This is a problem. The teacher asked us to write a simple exchange program and realized that three steps are different or two values can be exchanged. So I want to define a simple function outside the main function to implement the function call and then implement the function.
At the beginning, it was like this:
Swap (int A, int B)
{
A = a ^ B;
B = a ^ B;
A = a ^ B;
}
But after the call, I found that the values of A and B in the main function were not actually changed. It only used copies of the values in the main function. How can I solve this problem?
The solution is as follows:
I:
Main ()
{
Int A = 1, B = 2;
Swap (& A, & B );
}
Swap (int * a, int * B)
{
* A = * a ^ * B; // a simple example can be provided, for example, the difference between 0000 0001 and 0000 0010, or two values can be passed.
* B = * a ^ * B;
* A = * a ^ * B;
}
II:
It is also possible to use intermediate variables, but this is really old, we still need to strive for every day, and strive to write high-quality programs !!!