Home>

### Anatomy of the use of pointers as function parameters in C ++ programming

In C, one of the common uses of function pointer variables is as a function parameter.Pass function names to the formal parameters of other functions.In this way, different functions can be called in the process of calling a function according to different given arguments.

For example, using this method you can write a universal function that evaluates an integral,Use it to find the definite integral of 5 functions:

As can be seen,The function that requires a definite integral is different each time.You can write a general function integral for determining integrals, which has three formal parameters:a lower limit a, an upper limit b, and a pointer variable fun to the function. The function prototype can be written as:

`````` double integral (double a, double b, double (* fun) (double));
``````

Write five functions f1, f2, f3, f4, and f5, respectively, to find the values ​​of the above five functions.Then call the integral function 5 times in succession, and each time it takes a, b and f1, f2, f3, f4, f5 as actual parameters,That is, the upper and lower limits and the entry address of the relevant function are passed to the formal parameter fun. During the execution of the integral function, the value of the definite integral of each function is obtained.

In object-oriented C++ programming,This usage is relatively rare.

The parameters of a function can be not only integer, floating point, and character data,It can also be a pointer type.Its role is to pass the address of a variable to the formal parameter of the called function.

[Example] The two integers input are output in order of size.This is handled by functions,And use pointer-type data as function parameters.The procedure is as follows:

``````#include<iostream>
using namespace std;
int main ()
{
void swap (int * p1, int * p2);//function declaration
int * pointer_1, * pointer_2, a, b;//Define pointer variables pointer_1, pointer_2, integer variables a, b
cin>>a>>b;
pointer_1 =&a;//make pointer_1 point to a
pointer_2 =&b;//make pointer_2 point to b
if (a&b;b) swap (pointer_1, pointer_2);//if a<b, make * pointer_1 and * pointer_2 swap
cout<<"max ="<<a<"min ="<<b<endl;//a is already a large number,b is decimal
return 0;
}
void swap (int * p1, int * p2) //The function is to swap the value of * p1 with the value of * p2
{
int temp;
temp=* p1;
* p1=* p2;
* p2=temp;
}
``````

The operation is as follows:

``````45 78↙
max=78 min=45
``````

Please note:do not write the swap function call in the main function as

`````` if (a&b;b) swap (* pointer_1, * pointer_2);
``````

Notice how swapping the values ​​of * p1 and * p2 is implemented.If you write this:

``````void swap (int * p1, int * p2)
{
int * temp;
* temp=* p1;//There is a problem with this statement
* p1=* p2;
* p2=* temp;
}
``````

The approach taken in this example is to swap the values ​​of a and b, leaving the values ​​of p1 and p2 unchanged.

can be seen,After the swap function is executed, the values ​​of the variables a and b in the main function are changed.This change is not achieved by passing the parameter value back to the actual parameter.Readers are asked to consider whether a and b can be swapped by calling the following function.

``````void swap (int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
``````

What is the result of calling the swap function with "swap (a, b);" in the main function?When the function is called,The value of a is passed to x, and the value of b is passed to y. After executing the last statement of the swap function,The values ​​of x and y are interchanged,But a and b in the main function are not interchanged.As shown in Figure 6.10 (b). That is to say, because the combination of virtual and real is a one-way "value transfer" method, data can only be passed from the actual parameters to the formal parameters.Changes in parameter values ​​cannot be passed back to actual parameters.

In order to make the value of the variable changed in the function available to the main function,Can not take the method of changing the value as a parameter,Instead, use pointer variables as function parameters.Change the value of the variable pointed to by the pointer variable during the execution of the function,After the function call ends,Changes in the values ​​of these variables are still maintained,This achieves "change the value of a variable by calling a function,Use these changed values ​​in the calling function. "

If i want to get n values ​​to be changed through a function call,The following steps can be taken:

Set n variables in the calling function, and use n pointer variables to point to them;

Write the called function,Its formal parameters are n pointer variables,These formal parameter pointer variables should have the same base type as the n pointer variables in the calling function;

Take n pointer variables as arguments in the calling function,Pass their values ​​(address values) to the n formal parameter pointer variables of the called function,In this way, the formal parameter pointer variable also points to these n variables;

Through the pointer of the formal parameter pointer variable,Change the values ​​of the n variables;

Variables whose values ​​have changed can be used in the calling function.

Note that you cannot attempt to change the value of a parameter pointer variable by changing the value of a parameter pointer variable.Please analyze the following procedures:

``````#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;
}
void swap (int * p1, int * p2)
{
int * temp;
temp=p1;
p1=p2;
p2=temp;
}
``````

The data transfer between the argument variables and the formal parameters is a one-way "value transfer" method. Pointer variables must follow this rule as function parameters.The value of the argument pointer variable is not changed when the function is called,But you can change the value of the variable pointed to by the argument pointer variable.

Function calls can (and can only) get a return value (that is, the function value), and use pointer variables as function parameters,You can change the value of the variable in the calling function through the pointer variable.Equivalent to getting multiple values ​​from the called function through a function call.This is difficult to do without pointer variables.

[Example] Input 3 integers a, b, and c, and output them in ascending order.

Using the method described above,Use 3 pointer variables to point to 3 integer variables,Then use the swap function to swap the values ​​of the three integer variables.The procedure is as follows:

``````#include<iostream>
using namespace std;
int main ()
{
void exchange (int *, int *, int *);//Declaration of the exchange function
int a, b, c, * p1, * p2, * p3;
cin>>a>>b>>c;//Enter 3 integers
p1 =&a;p2 =&b;p3 =&c;//point to 3 integer variables
exchange (p1, p2, p3);//Exchange the values ​​of the three integer variables pointed to by p1, p2, p3
cout<a<""<b<""<c<endl;//output 3 integers in ascending order
}
void exchange (int * q1, int * q2, int * q3)
{
void swap (int *, int *);//Declaration of swap function
if (* q1<* q2) swap (q1, q2);//Call swap to swap the values ​​of the variables pointed to by q1 and q2
if (* q1<* q3) swap (q1, q3);//Call swap to swap the values ​​of the variables pointed to by q1 and q3
if (* q2<* q3) swap (q2, q3);//Call swap to swap the values ​​of the variables pointed to by q2 and q3
}
void swap (int * pt1, int * pt2) //Swap the values ​​of the variables pointed to by pt1 and pt2
{
int temp;
temp=* pt1;
* pt1=* pt2;
* pt2=temp;
}
``````

The operation is as follows:

``````12 -56 87↙
87 12 -56
``````
• Previous IE9 + has no backwards compatible solution for documentcreateElement
• Next Explain the mutual use of strings and arrays and pointers in C ++