Notes when passing C ++ pointers as function parameters

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.