The callback function I understand

What is a callback function : A callback function is a function called through a function pointer . If you pass a function pointer (address) as a parameter to another function, when this pointer is used to call the function it points to, we say this is a callback function. The callback function is not directly called by the implementer of the function, but is called by another party when a specific event or condition occurs, and is used to respond to the event or condition.

For example: There are two people, one is in charge of buying vegetables, and the other is in charge of cooking. The person who cooks has to wait for the buyer to buy the vegetables and put them in the kitchen before starting to cook. As for whether the dish is sour, sweet, or bitter, the person who cooks has the final say. It doesn't matter who buys the food, the person who buys the food only needs to provide the ingredients. Then in the callback function, the person who writes the callback function is equivalent to the person who buys vegetables, and the person who calls the function callback is equivalent to the person who cooks. The person who writes the callback function will notify the event when the event occurs, and pass the required values ​​(ingredients) together. The person who calls the callback function knows that this happened, and he can use these values ​​( ingredients) to achieve the function he wants to achieve.

The person who writes the callback function (the person who buys vegetables):

Step 1: Define the callback function type
typedef void (*GetInfoCallBack)(ingredients_info_t* info);

Step 2: Define the function GetInfoCallBack of the callback function type
get_info_callback = NULL

Step 3: Pass this callback function address to the interface function
get_info_ingredients(GetInfoCallBack callock) for you to obtain information (ingredients);

Step 4: Give the callback address of the third step to the callback function of the second step
get_info_ingredients(GetInfoCallBack callock)
{     get_info_callback = callock; }

Step 5: Pass the information (ingredients) you obtained through this callback function to the person who wants the information (cooking person)
(get_info_callback)(&info);

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>


typedef struct{
    char fish[64];
    char Pork[64];
    char Greens[64];
    int state;
}ingredients_info_t;

//第一步,定义回调函数类型
typedef void (*GetInfoCallBack)(ingredients_info_t* info); //一般在.h里面定义
//第二步:定义回调函数类型的函数
GetInfoCallBack get_ingredients_callback = NULL;  //一般在.c里面定义

//第三步:把这个回调函数地址传到你获取信息(食材)的接口函数
int get_info_ingredients(GetInfoCallBack callock)
{
    //第四步:把第三步的回调地址给第二步的回调函数
    get_ingredients_callback = callock;

    //买菜,获取食材
    ingredients_info_t info;
    memset(&info, 0, sizeof(ingredients_info_t));
    strcpy(info.fish, "fish");
    strcpy(info.Pork, "Pork");
    strcpy(info.Greens, "Greens");

    info.state = 1;//当值为1时,说明该事件发生了;菜已经买回来了

    //第五步:把你获取的信息(食材)通过这个回调函数传出去,给要这些信息的人(炒菜的人)
    (get_ingredients_callback)(&info);

}

The person who calls the callback function (the person who cooks) :

The first step is to call the interface to register a callback function

The second step is to realize the callback content when the information (ingredients) is obtained


//第二步,当获取到信息时(食材),实现回调内容
void Stir_fry_callnack(ingredients_info_t *info)
{
    if(info->state == 1){
        printf("我要开始做:%s\n", info->fish);
        printf("我要开始做:%s\n", info->Pork);
        printf("我要开始做:%s\n", info->Greens);
    }
}

int main(int argc, char const *argv[])
{
    //第一步,调用接口,并注册一个回调函数
    get_info_ingredients(Stir_fry_callnack);
    return 0;
}

The result of the call is as follows:

Guess you like

Origin blog.csdn.net/weixin_42432281/article/details/104563163