A brief introduction to the callback function in C language

Source: Internet
Author: User

This document describes the callback functions in the C language

Callback functions are a commonly used advanced application in C language and are widely used in library functions and applications.

Points:

1. Callback function Invocation method

2. The complete form of the callback function parameter transfer principle

Use of callback functions for 3.C language library functions

The concept and role of callback functions

A callback function is a commonly used method of function invocation in a program. It is characterized by the invocation of functions from the top software module (the macro caller) in the lower software module (the macro-caller), which is the opposite of the usual invocation order.

The relationship between the software module and the program's calling mode is divided into the upper software module and the lower software module in the overall view. There are generally three ways of calling

1. Simple synchronous call up direct return

2. Calls with callbacks call bidirectional

3. Asynchronously invoke a mechanism that is generally based on messages or events like MFC

The invocation with a callback is a two-way invocation pattern, in this way, the function of the lower module, when called, will call a function of the upper module, and the function that callback the upper module has at least two functions, one is to monitor the running state of the lower module function, and the other is to intervene the operation of the lower module function.

The basis of a callback function in C language is a pointer to a function type.

The function of the callback function the callback function is represented by a certain type of function pointer, which is implemented by the Upper software module, which passes the function pointer to the lower software module in some way, and is called by the lower software module at some point.

The essence of a function pointer is a letter address, which is essentially a 32-bit unsigned integer in a 32-bit system.

The transfer of function pointers from the upper module to the lower module can be passed through the parameters of the function, or it can be passed by members of the struct body.

There are generally two ways to call a callback function: one is to call the callback function directly in the function of the upper module calling the lower module: the other is to use the registration method to invoke the callback function in the lower module when an event occurs.

Syntax for callback functions:,

1. The simplest type of function is a function that has no parameters and returns no value. The simplest form of the callback function is this. Its type is defined as follows:

typedef void (*t_simple_callback) (void);

Note: The callback function can be any type of function pointer, in the simplest form, without any arguments or return a worthwhile function pointer

2. The full form of the callback function can be implemented as follows, it contains the parameters of two unsigned integers, and the return value is an unsigned integer. Its type is defined as follows:

typedef unsigned INT (*t_full_callback) (unsigned int para1,unsigned int para2);

Note: 1. A typical callback function contains two integer parameters and integer return values, the first parameter is defined by the lower level, the second is defined by the upper level, and the return value is the upper level that is returned by a callback to the lower

2. In fact, this callback mechanism is commonly used when the software is coordinated at various levels of a system. For example, the lower module represents a GUI system with the function of drawing the interface, the upper layer passes the pointer of the callback function to the lower level, and when the interface produces "event", the callback function is invoked to perform the behavior that the upper software module customizes.

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.