Outputs two integers for input in size order
The code is as follows:
#include <iostream>using namespace Std;int main () { void swap (int *p1,int *p2); int *pointer_1,*pointer_2,a,b; cin>>a>>b; pointer_1=&a; pointer_2=&b; if (a<b) swap (pointer_1,pointer_2); cout<< "max=" <<a<< "<<" min= "<<b<<endl; return 0;} <span style= "color: #ff0000;" >void Swap (int *p1,int *p2) { int temp; TEMP=*P1; *P1=*P2; *p2=temp;} </span>
Note: Use thevariablesWhen a function parameter is used, the combination of the real and the false is a one-way ' value pass ' that only passes the data from the parameter to the parameter, and the parameter value cannot be returned to the argument, that is, the value of the argument does not change because of the change of the parameter. (This rule should also be followed when a pointer variable makes a function argument, except when the character array name is a function parameter)What I want to say here is that when you do a function argument with a pointer variable, the value of the variable pointed to by the pointer changes during the execution of the function, and after the function call ends, changes to the values of these variables are preserved. This makes it possible to change the value of a variable by calling a function, and the changed values are used in the keynote function.
However, you cannot attempt to change the value of an argument pointer variable by changing the value of the parameter pointer variable. For example, this:
void swap (int *p1,int *p2) { int *temp; TEMP=P1; P1=P2; P2=temp;}
So calling a function does not change the value of the argument pointer variable, but it can change the value of the variable that the argument pointer variable is pointing to.
Copyright NOTICE: This article is my original article, we can reprint Oh!!
How to change the value of the main function variable when arguments and parameter pointers do function arguments