Function pointer example: void (* handle) (chainbintreee * P)

Source: Internet
Author: User
In C language, a function always occupies a consecutive memory zone, and the function name is the first address of the memory zone occupied by the function. We can assign the first address (or entry address) of the function to a pointer variable so that the pointer variable points to the function. Then, the pointer variable can be used to locate and call this function. We call this pointer variable pointing to a function "function pointer variable ".
Function pointer variables are defined as follows:
Type specifier (* pointer variable name )();
The "type specifier" indicates the type of the return value of the specified function. "(* Pointer variable name)" indicates that the variable after "*" is the defined pointer variable. The final empty parentheses indicate that the pointer variable refers to a function.
For example:
INT (* PF )();
PF is a pointer variable pointing to the function entry. The return value (function value) of this function is an integer.
[Example] This example describes how to use a pointer to call a function.
Int max (int A, int B ){
If (A> B) return;
Else return B;
}
Main (){
Int max (int A, int B );
INT (* Pmax )();
Int x, y, z;
Pmax = max;
Printf ("input two numbers: \ n ");
Scanf ("% d", & X, & Y );
Z = (* Pmax) (x, y );
Printf ("maxmum = % d", Z );
}

The procedure for calling a function in the form of a function pointer variable is as follows:
1) define the function pointer variable first, for example, the second line of INT (* Pmax) () in the next program, and define Pmax as the function pointer variable.
2) Assign the entry address (function name) of the called function to the function pointer variable, for example, 11th rows of Pmax = max in the program;
3) call a function in the form of a function pointer variable, such as the program's 14th line z = (* Pmax) (x, y );
4) The general form of calling a function is:
(* Pointer variable name) (real parameter table)
Note the following when using function pointer variables:
A) function pointer variables cannot be used for arithmetic operations, which is different from array pointer variables. Adding or subtracting an integer from an array pointer variable can move the pointer to an array element behind or before it, and moving the function pointer is meaningless.
B) In a function call, the brackets on both sides of "(* pointer variable name)" are indispensable. * should not be considered as a value calculation, where * is only a symbol.
 
 

There is a description in "C traps and defects" as follows:

FpIs a function pointer, then * fpIs the function pointed to by the pointer, so (* FP )()Is the method to call this function. ANSI CStandard allows programmers to abbreviated the above formula as FP ()But remember that this writing method is just a short form.

Function pointer example: void (* handle) (chainbintreee * P)

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.