Declare the function pointer and implement the callback \ Author: Danny Kalev, transferred from the http://www.vckbase.com/document/viewdoc? Id = 195 changed. Wait for a while before sorting it out.

Source: Internet
Author: User

Programmers often need to implement callback. This article discusses the basic principles of function pointers and describes how to use function pointers for callback. Note that this is intended for common functions, excluding class member functions that fully depend on different syntaxes and Semantic Rules (class member pointers will be discussed in another article ).

 

 

Function pointer variable:
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. Function names are automatically degraded into function pointers.
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 generally defined as: type specifiers (* pointer variable name )();
Where
The "type specifier" indicates the type of the return value of the specified function.
(* Pointer variable name) indicates * The variable following the definition is the pointer variable.
The final empty parentheses indicate that the pointer variable refers to a function.

To call a function as a function pointer, follow these steps:
1) define the function pointer variable first.
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 (* pointer variable name), the brackets on both sides are indispensable. * should not be considered as a value calculation, where * is only a symbol.

Declare function pointer

A callback function is a function that a programmer cannot explicitly call. It is called by passing the callback function address to the caller. To implement callback, you must first define the function pointer. Although the defined syntax is a bit incredible, if you are familiar with the general method of function declaration, you will find that the declaration of function pointers is very similar to that of function declaration. See the following example:

Void f (); // function prototype

The preceding statement declares a function. If no parameter is input, void is returned. The function pointer declaration method is as follows:

Void (*)();

Let's analyze that the asterisks in the left Circular Arc are the key to function pointer declaration. The other two elements are the return type (void) of the function and the entry parameter in the incircle arc (in this example, the parameter is null ). Note that no pointer variable is created in this example-only the variable type is declared. Currently, you can use this variable type to create a type definition name and use the sizeof expression to obtain the size of the function pointer:

// Obtain the size of the function pointer
Unsigned psize = sizeof (void (*)());

// Define the function pointer declaration type
Typedef void (* pfv )();

Pfv is a function pointer that points to a function without any input parameter and returns class behavior void. Using this type definition name can hide complex function pointer syntax.

The pointer variable should have a variable name:

Void (* p) (); // p is a pointer to a function.

P is a pointer to a function. This function has no input parameter and the return value type is void. The asterisk in the arc on the left is the pointer variable name. With pointer variables, you can assign values. The value contains the function name that matches the signature and the return type. For example:

Void func ()
{
/* Do something */
}
P = func;

The value assignment of p can be different, but it must be the address of the function, and the signature and return type are the same.

Pass the callback function address to the caller.

Now we can pass p to another function (caller)-caller (), which calls the function pointed to by p, and the function name is unknown:

Void caller (void (* ptr )())
{
Ptr ();/* call the function pointed to by ptr */
}
Void func ();
Int main ()
{
P = func;
Caller (p);/* pass the function address to the caller */
}

If different values are assigned to p (different function addresses), the caller will call functions with different addresses. Assign values can occur at runtime, so that you can achieve dynamic binding.

Call Specification

So far, we have only discussed function pointers and callbacks, but have not paid attention to the ansi c/C ++ compiler specifications. Many compilers have several call specifications. For example, in Visual C ++, you can add _ cdecl, _ stdcall, or _ pascal before the function type to indicate its call specifications (default value: _ cdecl ). C ++ Builder also supports the _ fastcall call specification. The call specification affects the given function name generated by the compiler, the sequence of parameter passing (from right to left or from left to right), and the responsibility for Stack cleaning (caller or called) and parameter transfer mechanism (stack, CPU register, etc ).

It is important to regard the call specification as part of the function type. Incompatible call specifications cannot be used to assign addresses to function pointers. For example:

// The called function uses int as the parameter and int as the return value.
_ Stdcall int callee (int );

// Call a function with the function pointer as the parameter
Void caller (_ cdecl int (* ptr) (int ));

// Illegal operation of the called function address in p's Enterprise Image Storage
_ Cdecl int (* p) (int) = callee; // Error

The pointer p and callee () types are incompatible because they have different call specifications. Therefore, the caller's address cannot be assigned to the pointer p, although the two have the same return value and parameter column.

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.