Example of passing C ++ Parameters

Source: Internet
Author: User
Void add (int * A) {(* A) = (* A)-5;} // pass the value, pass a copy of the real parameter as the function parameter to the void swapvalue (int A, int B) {int temp = A; A = B; B = temp;} function ;} // The pointer transmits the real parameter address to the function. Although a copy is passed in, the change is still the value on the real parameter address. Void swapbypointer (int * a, int * B) {int temp = * A; * A = * B; * B = temp;} // pass the reference, at this time, the address of the real variable that is passed to the function by the main function. // any operation of the called function on the form parameter is processed as indirect addressing. That is, the address stored in the stack is used to access the real variable in the main function. // Because of this, any operation performed by the called function on the form parameter affects the real Parameter Variable Void swapbyref (Int & A, Int & B) {int temp =; A = B; B = temp;} // transmits a reference to the pointer to exchange two pointers. Void swapbypointerref (int * & A, int * & B) {int * temp = A; A = B; B = temp;} int main () {int A = 10; int B = 20; int * pA = & A; int * pb = & B; cout <"A =" <A <"B =" <B <Endl; cout <"* pA =" <* Pa <"* pb =" <* pb <Endl; cout <"Pa =" <Pa <"PB =" <Pb <Endl; add (& ); cout <"= A =" <A <Endl; swapvalue (a, B); cout <Endl <"swapvalue \ n "; cout <"A =" <A <"B =" <B <Endl; cout <"* pA =" <* Pa <"* pb =" <* pb <Endl; cout <"Pa =" <Pa <"PB =" <Pb <Endl; swapbypointer (& A, & B ); cout <"\ n swapbypointer \ n"; cout <"A =" <A <"B =" <B <Endl; cout <"* pA =" <* Pa <"* pb =" <* pb <Endl; cout <"Pa =" <Pa <"PB =" <Pb <Endl; swapbyref (A, B ); cout <"\ n swapbyref \ n"; cout <"A =" <A <"B =" <B <Endl; cout <"* pA =" <* Pa <"* pb =" <* pb <Endl; cout <"Pa =" <Pa <"PB =" <Pb <Endl; swapbypointerref (Pa, Pb ); cout <"\ n swapbypointerref \ n"; cout <"A =" <A <"B =" <B <Endl; cout <"* pA =" <* Pa <"* pb =" <* pb <Endl; cout <"Pa =" <Pa <"PB =" <Pb <Endl; return 0 ;}
 
Output result: A = 10 B = 20 * pA = 10 * pb = 20 Pa = 0012ff60 Pb = 0012ff54 = A = 5 swapvaluea = 5 B = 20 * pA = 5 * pb = 20 pa = 0012ff60 Pb = 0012ff54 swapbypointera = 20 B = 5 * pA = 20 * pb = 5 Pa = 0012ff60 Pb = 0012ff54 swapbyrefa = 5 B = 20 * pA = 5 * pb = 20 Pa = 0012ff60 Pb = 0012ff54 swapbypointerrefa = 5 B = 20 * pA = 20 * pb = 5 Pa = 0012ff54 Pb = 0012ff60

 

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.