The callback function call procedure in C and the function pointer use

Source: Internet
Author: User

callback function Metaphor:

You go to a shop to buy things, just what you want to have no goods, so you left your phone in the shop clerk, after a few days the store has goods, the clerk called you, and then you received a call to the store to pick up the goods.

In this example, your phone number is called a callback function, you leave the phone to the clerk called the registration callback function, the store later in the goods called triggered the callback associated event, the clerk called you called Call callback function, you go to the store to fetch goods called response callback event.

A callback function is a function that a programmer cannot explicitly invoke, and a call is implemented by passing the address of the callback function to the caller.

callback function use is necessary, when we want to implement different content through a unified interface, it is very appropriate to use the back-off function.

For example, we wrote different display functions for several different devices:

void TVshow (); void Computershow (); void Notebookshow () ... Wait a minute.

This is where we want to use a uniform display function, and we can then use the back function. void Show (void (*PTR) ()); Different callback functions are called when used according to the parameters passed in.

Different programming languages may have different syntaxes, and here is an example of a callback function in C, where one callback function takes no arguments and the other callback function has parameters.

TEST.c#Include<stdlib.h>#Include<stdio.h>Int Test1(){int i;for (i=0; i<30; i++) {printf"The%dth charactor is:%c\ n", I, (Char) (' A ' +i%26)); }Return0;}Int Test2(IntNUM) {int i;for (i=0; i< num; i++) {printf"The%dth charactor is:%c\ n", I, (Char) (' A ' +i%26)); }Return0;}void Caller1(Int(*PTR)() )Pointers to functions as function arguments{(*ptr) ();}void Caller2(IntNInt(*PTR)(Int) )A pointer to a function as a function parameter, where the first argument is served for pointers to functions,{cannot be written as void Caller2 (int (*ptr) (int n)), which defines a syntax error. (*PTR) (n);}Int main () { printf ( "*************************** \n//equivalent to call Test1 (); printf ( "&&&&&& &**********************\n 5, Test2); //equivalent to call TEST2 (5); return 0;}     

The above is achieved by passing the address of the callback function to the caller, but it is important to note the use of the parameter callback function.

To implement a callback, you must first define a function pointer. The definition of a function pointer a little bit here. Like what:

int (*ptr)(); 

Here ptr is a function pointer where the parentheses (*PTR) cannot be omitted because the parentheses have precedence over the asterisk, which is a function declaration that returns the type of a pointer to an integral type.

Reference links
http://www.zhihu.com/question/19801131/answer/13005983

Http://www.blogjava.net/huyi2006/articles/180169.html

The callback function call procedure in C and the function pointer use

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.