
Due to changes in the public account’s push rules, please click “View” and add “Star” to get exciting technical shares as soon as possible
Source from the internet, infringement will be deleted
1. Function Pointers
int *p1;
char *p2;
STRUCT *p3; // STRUCT is the structure we defined
1. Concept
Return Type (* Pointer Variable Name) (Function Parameter List);
typedef Return Type (* Pointer Variable Name) (Function Parameter List);
typedef int (*Fun1)(int); // can also be written as int (*Fun1)(int x), but it is generally not done this way.
typedef int (*Fun2)(int, int); // two integers as parameters, returning an integer
typedef void (*Fun3)(void); // no parameters and return value
typedef void* (*Fun4)(void*); // both parameters and return value are void* pointers
2. How to Call Functions Using Function Pointers
int Func(int x); /* Declare a function */
int (*p) (int x); /* Define a function pointer */
p = Func; /* Assign the address of Func function to pointer variable p */
p = &Func /* Assign the address of Func function to pointer variable p */
Reply "C Language" in the public account [C Language Chinese Community] to receive 500G of high-quality programming materials for free
#include <stdio.h>
int Max(int, int); // Function declaration
int main(void)
{
int(*p)(int, int); // Define a function pointer
int a, b, c;
p = Max; // Assign the Max function to the pointer variable p, making p point to the Max function
printf("please enter a and b:");
scanf("%d%d", &a, &b);
c = (*p)(a, b); // Call the Max function through the function pointer
printf("a = %d\nb = %d\nmax = %d\n", a, b, c);
return 0;
}
int Max(int x, int y) // Define Max function
{
int z;
if (x > y)
{
z = x;
}
else
{
z = y;
}
return z;
}
p = Max can be changed to p = &Max
c = (*p)(a, b) can be changed to c = p(a, b)
3. Function Pointers as Parameters of a Function
#include <stdio.h>
#include <stdlib.h>
typedef void(*FunType)(int);
// Add the typedef keyword, this defines a function pointer type named FunType, not a FunType variable.
// The form is the same as typedef int* PINT;
void myFun(int x);
void hisFun(int x);
void herFun(int x);
void callFun(FunType fp,int x);
int main()
{
callFun(myFun,100);// Pass function pointer constant as a callback function
callFun(hisFun,200);
callFun(herFun,300);
return 0;
}
void callFun(FunType fp,int x)
{
fp(x);// Execute the passed function through fp's pointer; note that the function pointed to by fp has one parameter
}
void myFun(int x)
{
printf("myFun: %d\n",x);
}
void hisFun(int x)
{
printf("hisFun: %d\n",x);
}
void herFun(int x)
{
printf("herFun: %d\n",x);
}

4. Function Pointers as Function Return Types
void (* func5(int, int, float ))(int, int)
{
...
}
func5
takes (int, int, float)
as parameters, and its return type is void (*)(int, int)
. In C language, the declaration of variables or functions is also a great topic; to learn more about declarations, you can refer to my previous article – C Expert Programming Notes (Chapters 1-3). This book spent an entire chapter discussing how to understand C language declarations.5. Function Pointer Arrays
/* Method 1 */
void (*func_array_1[5])(int, int, float);
/* Method 2 */
typedef void (*p_func_array)(int, int, float);
p_func_array func_array_2[5];
<span>void (*)(int, int, float)</span>
*.6. Summary of Function Pointers
-
Function pointer constant: Max; function pointer variable: p; -
Function name calls should be like (*myFun)(10); otherwise, both writing and reading are inconvenient and unconventional. Therefore, the designers of C language allowed myFun(10) to be called (which is much more convenient and similar to mathematical function forms). -
Function pointer variables can also be stored in an array. The declaration method is: int (*fArray[10]) ( int );
2. Callback Functions
1. What is a Callback Function
A callback function is a function that is called via a function pointer. If you pass the pointer (address) of a function 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. A callback function is not directly called by the implementation party of that function but is called by another party when a specific event or condition occurs, used to respond to that event or condition.

<span>callback</span>
.<span>synchronous callback</span>
; if executed later, it is called an <span>asynchronous callback</span>
.<span>Callback functions</span>
are functions called via function pointers. If you pass the pointer (address) of a function 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.2. Why Use Callback Functions?

int Callback() ///< Callback function
{
// TODO
return 0;
}
int main() ///< Main function
{
// TODO
Library(Callback); ///< Library function calls back through function pointer
// TODO
return 0;
}
3. How to Use Callback Functions?
int Callback_1(int a) ///< Callback function 1
{
printf("Hello, this is Callback_1: a = %d ", a);
return 0;
}
int Callback_2(int b) ///< Callback function 2
{
printf("Hello, this is Callback_2: b = %d ", b);
return 0;
}
int Callback_3(int c) ///< Callback function 3
{
printf("Hello, this is Callback_3: c = %d ", c);
return 0;
}
int Handle(int x, int (*Callback)(int)) ///< Note the function pointer definition used here
{
Callback(x);
}
int main()
{
Handle(4, Callback_1);
Handle(5, Callback_2);
Handle(6, Callback_3);
return 0;
}
<span>Handle()</span>
function is a pointer. When calling the <span>Handle()</span>
function in the <span>main()</span>
function, we passed the function names <span>Callback_1()/Callback_2()/Callback_3()</span>
as parameters, and the function names here are the corresponding function pointers. In other words, callback functions are a way to use function pointers.4. Here is a simple callback function example for basic arithmetic operations:
#include <stdio.h>
#include <stdlib.h>
/****************************************
* Function Pointer Structure
***************************************/
typedef struct _OP {
float (*p_add)(float, float);
float (*p_sub)(float, float);
float (*p_mul)(float, float);
float (*p_div)(float, float);
} OP;
/****************************************
* Addition, Subtraction, Multiplication, Division Functions
***************************************/
float ADD(float a, float b)
{
return a + b;
}
float SUB(float a, float b)
{
return a - b;
}
}
float MUL(float a, float b)
{
return a * b;
}
float DIV(float a, float b)
{
return a / b;
}
/****************************************
* Initialize Function Pointers
***************************************/
void init_op(OP *op)
{
op->p_add = ADD;
op->p_sub = SUB;
op->p_mul = &MUL
op->p_div = &DIV
}
/****************************************
* Library Function
***************************************/
float add_sub_mul_div(float a, float b, float (*op_func)(float, float))
{
return (*op_func)(a, b);
}
int main(int argc, char *argv[])
{
OP *op = (OP *)malloc(sizeof(OP));
init_op(op);
/* Directly use function pointers to call functions */
printf("ADD = %f, SUB = %f, MUL = %f, DIV = %f\n", (op->p_add)(1.3, 2.2), (*op->p_sub)(1.3, 2.2),
(op->p_mul)(1.3, 2.2), (*op->p_div)(1.3, 2.2));
/* Call callback functions */
printf("ADD = %f, SUB = %f, MUL = %f, DIV = %f\n",
add_sub_mul_div(1.3, 2.2, ADD),
add_sub_mul_div(1.3, 2.2, SUB),
add_sub_mul_div(1.3, 2.2, MUL),
add_sub_mul_div(1.3, 2.2, DIV));
return 0;
}
5. Callback Function Example (Very Useful)
/********* Work Status Processing *********/
typedef struct
{
uint8_t mStatus;
uint8_t (* Function)(void); // In the form of function pointers
} M26_WorkStatus_TypeDef; // Collection of M26 working status calling functions
/**********************************************
** > M26 Work Status Collection Functions
**********************************************/
M26_WorkStatus_TypeDef M26_WorkStatus_Tab[] =
{
{GPRS_NETWORK_CLOSE, M26_PWRKEY_Off }, // Module power off
{GPRS_NETWORK_OPEN, M26_PWRKEY_On }, // Module power on
{GPRS_NETWORK_Start, M26_Work_Init }, // Pin initialization
{GPRS_NETWORK_CONF, M26_NET_Config }, // AT command configuration
{GPRS_NETWORK_LINK_CTC, M26_LINK_CTC }, // Connect to the scheduling center
{GPRS_NETWORK_WAIT_CTC, M26_WAIT_CTC }, // Wait for the scheduling center's reply
{GPRS_NETWORK_LINK_FEM, M26_LINK_FEM }, // Connect to the front machine
{GPRS_NETWORK_WAIT_FEM, M26_WAIT_FEM }, // Wait for the front machine's reply
{GPRS_NETWORK_COMM, M26_COMM }, // Normal operation
{GPRS_NETWORK_WAIT_Sig, M26_WAIT_Sig }, // Wait for signal reply
{GPRS_NETWORK_GetSignal, M26_GetSignal }, // Get signal value
{GPRS_NETWORK_RESTART, M26_RESET }, // Module restart
}
/**********************************************
** > M26 Module State Machine, calling the 12 functions inside sequentially
**********************************************/
uint8_t M26_WorkStatus_Call(uint8_t Start)
{
uint8_t i = 0;
for(i = 0; i < 12; i++)
{
if(Start == M26_WorkStatus_Tab[i].mStatus)
{
return M26_WorkStatus_Tab[i].Function();
}
}
return 0;
}
If you are over 18 years old and find learning [C language] too difficult? Want to try other programming languages? I recommend you learn Python; currently, a Python zero-based course worth 499 yuan is available for free, limited to 10 spots!
▲ Scan the QR code to receive it for free
Recommended Reading
C Language Callback Functions, Essential Tips to Enhance C Skills
Those C Language Knowledge Points That Are Easy to Learn but Hard to Remember
I wish I had known these before learning C Language!
Starting Programming with C Language, These Reasons Must Be Known!
Click Read Original to learn more