callback functions and function pointers

Source: Internet
Author: User

First, function pointers:

(1) A function pointer is a pointer variable that points to a function. That is, it is a pointer variable, and the pointer points to a function.

For a pointer variable, its value is the address of the variable it points to. For example: The pointer variable pi is a pointer to an integer variable I, then the address of the variable i &i is the value of the pointer variable pi. This means that the integer variable pointer points to an integer variable, and the value of the integer variable pointer is the address of the integer variable it refers to. As with other types of pointer variables, the value of a function pointer variable is the address of the function it points to.


So what is the address of the function?

Let's start by looking at how the function call is going. A function consumes a contiguous amount of memory while the program is running. When a function is called, it actually jumps to the entry address of the function, executes the code of the function body, and returns when it is finished.

The function pointer points to the entry address of a function, which is the first address of the function storage space.

In the C language, the array name represents the first address of the array, and the same function name represents the first address, so the function pointer is directly directed to the function name when the value is assigned.

(2) Definition of function pointers

In general, the function pointer is defined in the following format:

function type (* pointer variable name) (parameter list);

The function type describes the return type of the function, because the precedence of "()" is higher than "*", so the parentheses outside the pointer variable name are necessary, and the subsequent "parameter list" represents the parameter list of the function that the pointer variable points to.

For example, for the function int f (int a), we define a function pointer fp to the function in the following format:

Int (*FP) (int a);


Second, the callback function:

650) this.width=650; "src=" Http://img.blog.163.com/photo/vHklLy0je1URDlOfPfOCzQ==/5757852123593628019.jpg "alt=" VC + + function pointer and callback function "title=" VC + + function pointer and callback function "style=" border:0px; "/>

Figure 1

In Figure 1, it is easy to understand that it is the case that we call the function normally, and the function is called directly in the program.

650) this.width=650; "src=" Http://img.blog.163.com/photo/RCHMAoxhdDzGSCV3tGal0g==/5757852123593628020.jpg "alt=" VC + + function pointer and callback function "title=" VC + + function pointer and callback function "style=" border:0px; "/>

Figure 2

The understanding of Figure 2 is somewhat puzzling, first the program a calls the function 1, and the function 1 itself calls the program A to write its own function 2. Function 2 is the callback function.

The first time to understand the callback function is really difficult, in order to let you understand it, I am here to explain in Figure 2:

In general, function 1 is done by the system. Figure 2 is changed to be easier to understand:

650) this.width=650; "src=" Http://img.blog.163.com/photo/5biQxjEFOXm463Tm6fcnxA==/5757852123593628021.jpg "alt=" VC + + function pointer and callback function "title=" VC + + function pointer and callback function "style=" border:0px; "/>

Figure 3

Our program performs an operation, notifies the system, calls the system's function 1 to perform the function, but the system needs to call function 2, and the contents of function 2 are done by us.

For example, we press a button, we call the system (can also say the notification system) to complete the corresponding function, the system first completes the button redraw, so that it appears as pressed state, but also to perform the button is pressed after the corresponding action. button is pressed after the operation of the content is done by the user, the system is not certain, the user can fill in the content according to their own needs.

It is implemented by the program developer, but is called by the system (or other functions), which makes programming more convenient and flexible.





This article from "Liveyoung" blog, reproduced please contact the author!

callback functions and function pointers

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.