The C language points to the function pointer operation explanation, points to the function pointer

Source: Internet
Author: User
Tags float max

The C language points to the function pointer operation explanation, points to the function pointer

1. After the definition and calling program is compiled, each function has a first address (that is, the address of the first instruction of the function). This address is called the pointer of the function. You can define the pointer variable pointing to the function and call the function indirectly using the pointer variable. The following is a simple example:

float max(float x,float y){return x>y?x:y;}float min(float x,float y){return x

The running result is: max = 2.000000

Min = 1.000000

Note:

(1) Statement float (* p) (float x, float y); defines a pointer variable pointing to the function. The function format is: the return value is float type, and the list of formal parameters is (float x, float y ). After p is defined, it can point to any function that meets the format.

(2) The format of the pointer variable defining the pointer to the function is:

(3) data type (* pointer variable name) (Form parameter list );

(4) the data type is the type of the function return value, and the form parameter list is the form parameter list of the function.

(5) In the formal parameter list, the parameter name can be omitted. For example, float (* p) (float x, float y); can be written as: float (* p) (float, float );

(7) Note that the brackets on both sides of the pointer variable name cannot be omitted.

(8) Statement p = max; assign the first address value of the max function to the pointer Variable p, that is, point p to the function max. In C, the function name represents the first address of the function.

(9) The first c = (* p) (a, B); Statement: Because p points to the first address of the max function, (* p) (a, B) it is equivalent to max (a, B ). Function returns 2.0. Note * brackets on both sides of p cannot be omitted.

(10) Statement p = min; assign the first address value of the min function to the pointer Variable p. P is a variable. The value of p is actually a memory address value. It can point to max or min, but the format of the function must be consistent with the definition of p.

(11) second c = (* p) (a, B); Statement: Because p points to the first address of the min function, (* p) (a, B) it is equivalent to min (a, B ). Function returns 1.0.

(12) when the first address of the function is assigned to the pointer variable, you can directly write the function name without writing brackets and function parameters.

(13) when using pointer variables to call a function, you must specify the actual parameters of the function.

Tip: when defining a pointer variable pointing to a function, brackets must be used. Compare the following two definitions: float (* p1) (int x, long y); float * p2 (int x, long y ); the first statement defines a pointer variable p1 pointing to the function. The second statement declares a function p2. The formal parameter of p2 is (int x, long y ), the returned value is a float pointer.

2. When a pointer to a function is used as a function parameter, many functions are different, but their return values and list of formal parameters are the same. In this case, you can construct a general function and use the pointer of the function as the function parameter, which is conducive to the modular design of the program. For example, in the following example, four functions for adding, subtracting, multiplication, and Division Two float types are summed up into a mathematical operation function MathFunc. In this way, when the MathFunc function is called, as long as the specific function name is used as the actual parameter of the function, MathFunc automatically calls the corresponding addition, subtraction, multiplication, and Division functions and calculates the result. The following is the code of the program:

float Plus(float f1, float f2);float Minus(float f1, float f2);float Multiply(float f1, float f2);float Divide(float f1, float f2);float MathFunc(float (*p)(float, float), float para1,float para2);main(){float a=1.5, b=2.5;printf("\na+b=%f", MathFunc(Plus, a,b));printf("\na-b=%f", MathFunc(Minus, a,b));printf("\na*b=%f", MathFunc(Multiply, a,b));printf("\na/b=%f", MathFunc(Divide, a,b));}float Plus(float f1, float f2){return f1+f2;}float Minus(float f1, float f2){return f1-f2;}float Multiply(float f1, float f2){return f1*f2;}float Divide(float f1, float f2){return f1/f2;}float MathFunc(float (*p)(float, float), float para1,float para2){return (*p)( para1, para2);}

The program runs as follows:

A + B = 4.000000

A-B =-1.000000

A * B = 3.750000

A/B = 0.600000

Related Article

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.