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