Notes:
When a pointer is passed as a function parameter, it is essentially a "value transfer", that is, a new pointer variable pointing to the address is copied.
Only when the pointer is referenced in the called function can the variable pointed to by the pointer be changed as long as the return value is not required.
The following two examples are analyzed;
Requirement: define and initialize two string variables and execute the output operation. Then, call the function to exchange the values of these two variables, and pass the pointer to the value of the called function.
Program 1.1
[Cpp]
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
String str1 = "I love China! ", Str2 =" I love JiNan! ";
Void Exchange (string * p1, string * p2 );
Cout <"str1:" <str1 <endl;
Cout <"str2:" <str2 <endl;
Exchange (& str1, & str2 );
Cout <"str1:" <str1 <endl;
Cout <"str2:" <str2 <endl;
Return 0;
}
Void Exchange (string * p1, string * p2 ){
String * p3;
P3 = p1;
P1 = p2;
P2 = p3;
}
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
String str1 = "I love China! ", Str2 =" I love JiNan! ";
Void Exchange (string * p1, string * p2 );
Cout <"str1:" <str1 <endl;
Cout <"str2:" <str2 <endl;
Exchange (& str1, & str2 );
Cout <"str1:" <str1 <endl;
Cout <"str2:" <str2 <endl;
Return 0;
}
Void Exchange (string * p1, string * p2 ){
String * p3;
P3 = p1;
P1 = p2;
P2 = p3;
}
Output result:
Program 1.2
[Cpp]
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
String str1 = "I love China! ", Str2 =" I love JiNan! ";
Void Exchange (string * p1, string * p2 );
Cout <"str1:" <str1 <endl;
Cout <"str2:" <str2 <endl;
Exchange (& str1, & str2 );
Cout <"str1:" <str1 <endl;
Cout <"str2:" <str2 <endl;
Cout <endl;
Return 0;
}
Void Exchange (string * p1, string * p2 ){
String p3;
P3 = * p1;
* P1 = * p2;
* P2 = p3;
}
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
String str1 = "I love China! ", Str2 =" I love JiNan! ";
Void Exchange (string * p1, string * p2 );
Cout <"str1:" <str1 <endl;
Cout <"str2:" <str2 <endl;
Exchange (& str1, & str2 );
Cout <"str1:" <str1 <endl;
Cout <"str2:" <str2 <endl;
Cout <endl;
Return 0;
}
Void Exchange (string * p1, string * p2 ){
String p3;
P3 = * p1;
* P1 = * p2;
* P2 = p3;
}
Output result:
Analysis:
Through the comparison of the results of the two programs, the functions in program 1.1 fail to exchange values, while program 1.2 does;
In the main function, the main function transmits the address of the first element of str1 and str2 to the function Exchange function as a real parameter. In the Exchange function, p1 is used to receive the address of str1, p2 is used to receive the str2 address. This process is a value transfer.
In program 1.1, only the pointer p1 and pointer p2 are exchanged, which has no effect on the original strings str1 and str2. In program 1.2, * p1 and * p2 values are exchanged, while * p1 is str1 and * p2 is str2. Therefore, str1 and str2 are actually exchanged.