Parsing the use of pointers as function parameters in C + + programming _c language

Source: Internet
Author: User
Tags function prototype

In C, one of the common uses of function pointer variables is to pass the function name to the formal parameters of the other function as a function parameter. This allows you to call a different function in the process of calling a function based on the given different arguments.

For example, by using this method, we can write a general function of the definite integral, and use it to find the definite integral of 5 functions:

As you can see, the functions that require a definite integral each time are different. You can write a universal function integral, which has 3 formal parameters: the lower bound A, the upper limit B, and the pointer variable fun that points to the function. The function prototype can be written as:

  Double integral (double A, double b, double (*fun) (double));


Write 5 function F1,f2,f3,f4,f5 to find the value of the above 5 functions. Then call the integral function 5 times, each time the a,b and F1,f2,f3,f4,f5 as one of the arguments, that is, the upper limit, the lower bound and the function of the entry address to the formal parameter fun. The value of the definite integral of each function is obtained in the process of executing the integral function.

In object-oriented C + + programming, this usage is relatively few.
The parameters of a function can be not only integers, floating-point types, character types, but also pointers. Its role is to pass the address of a variable to the parameters of the called function.

The "example" outputs the two integers entered in order of size. This is done using functions and using 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 variable Pointer_1,pointer_ 2, integral type variable a,b
  cin>>a>>b;
  pointer_1=&a; Make the pointer_1 point
  to a pointer_2=&b;//make the pointer_2 point
  to B if (a<b) swap (pointer_1,pointer_2);//if a<b, make * Pointer_1 and *pointer_2 interchange
  cout<< "max=" <<a<< "min=" <<b<<endl;//a is already a large number, B is a decimal
  return 0;
}
The effect of Void swap (int *p1,int *p2)//function is to exchange the value of *P1 with *p2 value of
{
  int temp;
  TEMP=*P1;    
  *P1=*P2;
  *p2=temp;     
}

The operating conditions are as follows:

45 78↙
max=78 min=45

Please note: Do not write the swap function call in the main function

  if (a<b) swap (*pointer_1, *pointer_2);



Notice how the value of the Exchange *P1 and *P2 is implemented. If you write the following, you have a problem:

void swap (int *p1, int *p2)
{
  int *temp;
  *TEMP=*P1; This statement has a problem
  *p1=*p2;
  *p2=*temp;      
}

This example takes the approach of exchanging the values of A and B, while the values of P1 and P2 are unchanged.

As you can see, the value of variables A and B in the main function changes after the SWAP function is executed. This change is not achieved by passing the parameter value back to the argument. Ask the reader to consider whether a and B swaps can be implemented by calling the following function.

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

In the main function, use "Swap (A, b);" Call the swap function, what's the result? When a function is called, the value of a is passed to the value of x,b to the Y. After the last statement of the SWAP function is executed, the values for x and Y are interchanged, but A and B in the main function are not interchangeable, as shown in Figure 6.10 (b). In other words, because the combination of the actual situation is to take one-way "value transfer" mode, only from the parameter to the formal parameter data, the change of parameters can not be passed back to the argument.

In order to change the value of the variable in the function can be used in the main function, can not take the variable to change the value of the argument as a method, but should use the pointer variable as a function parameter. When the function is executed, the variable values that the pointer variable points to are changed, and the change of the value of the variable remains after the function call is completed, thus realizing the purpose of "changing the value of the variable by calling the function and using these changed values in the keynote function."

If you want to get n a value that you want to change by using a function call, you can take the following steps:
Set n variables in the keynote function and point to them with n pointer variables;
Write the called function, whose formal parameters are n pointer variables, which should have the same base type as n pointer variables in the calling function;
In the calling function, n pointer variables are arguments, and their values (which are address values) are passed to the n parameter pointer variables of the called function, so that the parameter pointer variable also points to the n variables;
The value of the n variable is changed by pointing the parameter pointer variable;
You can use these variables that change the value in the keynote function.

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

#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 variable and the shape parametric is a one-way "value delivery" approach. The pointer variable as a function parameter also follows this rule. Calling a function does not change the value of the argument pointer variable, but it can change the value of the variable to which the argument pointer variable is pointing.

The invocation of a function can (and only can) get a return value (that is, a function value), and using a pointer variable as a function parameter, you can change the value of the variable in the calling function by the pointer variable, which is equivalent to getting multiple values from the called function through a function call. It is difficult to do this without using pointer variables.

"Example" input a,b,c 3 integers, output in order from large to small.

Using the method described above, point to 3 integer variables with 3 pointer variables, and then swap the value of 3 integer variables with the swap function. The procedure is as follows:

#include <iostream>
using namespace std;
int main ()
{
  void exchange (int *,int *,int *);//declaration int A,B,C,*P1,*P2,*P3 for Exchange functions
  ;
  cin>>a>>b>>c; Enter 3 integer
  p1=&a;p2=&b;p3=&c;//point to 3 Integer variable
  Exchange (P1,P2,P3), or the value of the 3 integer variable to which the P1,P2,P3 points
  cout <<a<< "" <<b<< "" <<c<<endl; Output 3 integers in a large to small order
}
void Exchange (int *q1,int *q2,int *q3)
{
  void swap (int *,int *);//Declaration of the Swap function C14/>if (*Q1<*Q2) swap (Q1,Q2); Call swap, which swaps the value of the variable that the Q1 is pointing to Q2 the
  if (*q1<*q3) swap (Q1,Q3);//Call swap, which swaps the value of the variable that the Q1 points to the
  if (Q3) swap (*Q2<*Q3, Q3); Call swap to swap the value of the variable Q2 with the Q3
}
void swap (int *pt1,int *pt2)//Convert the PT1 to the value of the variable that pt2 is pointing to
{
  int temp;
  TEMP=*PT1;
  *pt1=*pt2;
  *pt2=temp;
}

The operating conditions are as follows:

12-56 87↙
87 12-56

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.