First, let's look at the following small programs:
(Input two numbers and input them in order of size. -- check whether the output value can reach the goal)
// Value transfer
# Include <iostream>
Using namespace std;
Int main ()
{
Void max (int a, int B );
Int x, y;
Cin> x> y;
Max (x, y );
Cout <x <endl <y <endl;
Return 0;
} Www.2cto.com
Void max (int a, int B)
{
Int temp;
If (a <B)
{
Temp =;
A = B;
B = temp;
}
}
// Pointer Transmission
# Include <iostream>
Using namespace std;
Int main ()
{
Void max (int * p1, int * p2 );
Int x, y;
Int * point_1, * point_2;
Cin> x> y;
Point_1 = & x;
Point_2 = & y;
Max (point_1, point_2 );
Cout <x <endl <y <endl;
Return 0;
}
Void max (int * p1, int * p2)
{
Int temp;
If (* p1 <* p2)
{
Temp = * p1;
* P1 = * p2;
* P2 = temp;
}
}
// Array Transfer
# Include <iostream>
Using namespace std;
Int main ()
{
Void max (int a []);
Int a [2];
Cin> a [0]> a [1];
Max ();
Cout <a [0] <endl <a [1] <endl;
Return 0;
}
Void max (int a [])
{
Int temp;
If (a [0] <a [1])
{
Temp = a [0];
A [0] = a [1];
A [1] = temp;
}
}
// Reference Transmission
# Include <iostream>
Using namespace std;
Int main ()
{
Void max (int &, int &);
Int x, y;
Cin> x> y;
Max (x, y );
Cout <x <endl <y <endl;
Return 0;
}
Void max (int & a, int & B)
{
Int temp;
If (a <B)
{
Temp =;
A = B;
B = temp;
}
}
It is not difficult to find that the first Han number cannot reach the expected goal, and the other three can. A smart person can see at a glance that the first input parameter of the Han number is the value. Other
Can be considered as an address (the pointer naturally does not have to say that the array transmits the first address, and the reference is the copy of the address ).