[Daily Record] Function pointer and callback function

 

1. Function pointer and its definition and usage 

1. What is a function pointer?

2. How to call a function with a function pointer

Two, callback function (callback)

1. What is a callback function?

2. Why use callback function?

3. How to use the callback function?

4. How to use the callback function with parameters?

 


1. Function pointer and its definition and usage 

Reference link: Function pointer and its definition and usage, C language function pointer detailed explanation

1. What is a function pointer?

If a function is defined in the program, the system will allocate a section of storage space for the function code at compile time . The first address of this section of storage space is called the address of the function. And the function name represents this address. Since it is an address, we can define a pointer variable to store it. This pointer variable is called a function pointer variable , or function pointer for short .

So how to define this function pointer variable? Although it also points to an address, the pointer variable pointing to the function is defined differently from the pointer variable pointing to the variable we talked about earlier. E.g:

指向变量的指针变量:int (*p);

指向函数的指针变量:int (*p)(int, int);

This statement defines a pointer variable p that points to the function. First of all, it is a pointer variable, so there must be a "*", that is (*p); secondly, the int in the front indicates that the pointer variable can point to a function whose return value type is int; the two ints in the back bracket indicate the pointer Variables can point to functions that have two parameters and both are of type int. So the combined meaning of this statement is: a pointer variable p is defined, which can point to a function whose return value type is int type and has two integer parameters. The type of p is int(*)(int, int).

So the definition of function pointer is:

Function return value type (* pointer variable name) (function parameter list);

 "Function return value type" indicates that the pointer variable can point to a function with what return value type; "function parameter list" indicates that the pointer variable can point to a function with what parameter list. This parameter list only needs to write the parameter type of the function .

Small Tip: The definition of function pointer is to change the "function name" in the "function declaration" to "(*pointer variable name)". example:

函数是:  int max(int a,int b);

函数指针:int (*p)(int,int);

note:

1. The parentheses at both ends of "(*pointer variable name)" cannot be omitted. The parentheses change the priority of operators. If the parentheses are omitted, it is not a function pointer but a function declaration, that is, a function whose return value type is a pointer type is declared.

2. The difference between a function pointer and a pointer to a variable is that there are parentheses with formal parameter types behind the function pointer variable name.

3. The function pointer does not have ++ and - operations.

2. How to call a function with a function pointer

For the function max(int,int), the values ​​of max and &max are the same. So adding or not adding & is the same.

#include <stdio.h>
 
int max(int x, int y)
{
    return x > y ? x : y;
}
 
int main(void)
{
    /* p 是函数指针 */
    int (* p)(int, int) =  &max; // &可以省略
    int a, b, c, d;
 
    printf("请输入三个数字:");
    scanf("%d %d %d", & a, & b, & c);
 
    /* 与直接调用函数等价,d = max(max(a, b), c) */
    d = p(p(a, b), c); 
 
    printf("最大的数字是: %d\n", d);
 
    return 0;
}

After compiling and running, the results are as follows:

请输入三个数字:1 2 3
最大的数字是: 3

Two, callback function (callback)

Reference link: C language callback function explain the   popular understanding of "callback function"

1. What is a callback function?

Let's take a look at the English definition of callback:

A callback is a function that is passed as an argument to another function and is executed after its parent function has completed。

Literally understood, a callback function is a parameter. Pass this function as a parameter to another function. After the function is executed, the passed function is executed. That is to say, when function F1 calls function F2, function F1 passes another function F3 pointer to function F2 through parameters. During the execution of function F2, function F2 calls function F3. This action is called callback (Callback ), and the function F3, which is first passed as a pointer and then called back, is the callback function.

Give an example that others have given:

After the date, you send your girlfriend home. When you leave, you will definitely say: "Send me a message when you get home. I am very worried about you." No, then your girlfriend really sent you a message when she got home. a message. Boy, you have a show. In fact, this is a callback process. You leave a parameter function (requesting your girlfriend to send you a message) for your girlfriend, and then your girlfriend goes home. The action to go home is the main function. She must first return home after the main function is executed, and then execute the passed function, and then you will receive a message.

2. Why use callback function?

Many friends may think, why not write the name of the function directly in the callback place like ordinary function call? Isn’t that okay? Why do we have to use a callback function? It's good to have this idea, because I saw many examples of parsing callback functions on the Internet, but they can actually be implemented with ordinary function calls. To answer this question, let's first understand the benefits and functions of returning to functions, that is, decoupling. Yes, it is such a simple answer. Because of this feature, ordinary functions cannot replace callback functions. Therefore, in my eyes, this is the biggest feature of the callback function. Take a look at a picture on Wikipedia that I think is well drawn.

Detailed C language callback function

 The following is an incomplete C language code to show the meaning of the above picture:

#include<stdio.h>
#include<softwareLib.h> // 包含Library Function所在读得Software library库的头文件
 
int Callback() // Callback Function
{
    // TODO
    return 0;
}
 
int main() // Main program
{ 
    // TODO
    Library(Callback);
    // TODO
    return 0;
}

At first glance, callbacks seem to be just calls between functions, which are no different from ordinary function calls, but a closer look reveals a key difference between the two: in the callback, the main program passes the callback function like a parameter Inbound function. In this way, as long as we change the parameters passed into the library function, we can achieve different functions. Does this feel very flexible? And there is no need to modify the implementation of library functions at all, which is decoupling. Take a closer look. The main function and the callback function are on the same layer, while the library function is on another layer. Think about it. If the library function is not visible to us, we cannot modify the implementation of the library function, which means that it cannot be modified. The library function allows the library function to call the ordinary function, then we can only pass in different callback functions, which is a common situation in daily work. Now put the main(), Library() and Callback() functions back into the previous F1, F2, and F3 functions. Isn't it clearer?

After understanding the characteristics of the callback function, is it possible to roughly know under what circumstances it should be used? Yes, you can use callback functions in many places instead of ordinary function calls, but in my opinion, if you need to reduce coupling, you should use callback functions.

3. How to use the callback function?

Knowing what a callback function is, and understanding the characteristics of the callback function, how should the callback function be used? Let's look at a simple synchronous callback function code that can be executed.

#include<stdio.h>

int Callback_1() // Callback Function 1

{

    printf("Hello, this is Callback_1 ");

    return 0;

}

int Callback_2() // Callback Function 2

{

    printf("Hello, this is Callback_2 ");

    return 0;

}

int Callback_3() // Callback Function 3

{

    printf("Hello, this is Callback_3 ");
        
    return 0;

}

int Handle(int (*Callback)())

{

    printf("Entering Handle Function. ");
    
    Callback();

    printf("Leaving Handle Function. ");

}

int main()

{

    printf("Entering Main Function. ");

    Handle(Callback_1);

    Handle(Callback_2);

    Handle(Callback_3);
    
    printf("Leaving Main Function. ");

    return 0;

}
运行结果:

Entering Main Function.

Entering Handle Function.

Hello, this is Callback_1

Leaving Handle Function.

Entering Handle Function.

Hello, this is Callback_2

Leaving Handle Function.

Entering Handle Function.

Hello, this is Callback_3

Leaving Handle Function.

Leaving Main Function.

As you can see, the parameter in the Handle() function is a pointer. When the Handle() function is called in the main() function, it is passed the function name of the function Callback_1()/Callback_2()/Callback_3(). The function name at this time is the pointer to the corresponding function, that is to say, the callback function is actually a usage of the function pointer. Now read this sentence again: A "callback" is any function that is called by another function which takes the first function as a parameter, is it clearer?

4. How to use the callback function with parameters?

Sharp-eyed friends may have discovered that the callback function in the previous example has no parameters, so can we call back those functions with parameters? The answer is yes. So how to call it? We can slightly modify the above example:

#include<stdio.h>

int Callback_1(int x) // Callback Function 1

{

    printf("Hello, this is Callback_1: x = %d ", x);

    return 0;

}

int Callback_2(int x) // Callback Function 2

{

    printf("Hello, this is Callback_2: x = %d ", x);

    return 0;

}

int Callback_3(int x) // Callback Function 3

{

    printf("Hello, this is Callback_3: x = %d ", x);

    return 0;

}

int Handle(int y, int (*Callback)(int))

{

    printf("Entering Handle Function. ");

    Callback(y);

    printf("Leaving Handle Function. ");
    
}

int main()

{

    int a = 2;

    int b = 4;

    int c = 6;

    printf("Entering Main Function. ");

    Handle(a, Callback_1);

    Handle(b, Callback_2);

    Handle(c, Callback_3);

    printf("Leaving Main Function. ");

    return 0;

}
运行结果:

Entering Main Function.

Entering Handle Function.

Hello, this is Callback_1: x = 2

Leaving Handle Function.

Entering Handle Function.

Hello, this is Callback_2: x = 4

Leaving Handle Function.

Entering Handle Function.

Hello, this is Callback_3: x = 6

Leaving Handle Function.

Leaving Main Function.

As you can see, it is not possible to directly change int Handle(int (*Callback)()) to int Handle(int (*Callback)(int)), but to save the parameters of the callback function by adding another parameter Value, like the parameter y of int Handle(int y, int (*Callback)(int)) here. Similarly, you can use a callback function with multiple parameters.

Guess you like

Origin blog.csdn.net/qq_27148893/article/details/109275852