C语言回调函数

时间:2023-03-09 17:34:13
C语言回调函数

Callbacks have a wide variety of uses. For example, imagine a function that reads a configuration file and associates values with options. If the options are identified by a hash, then writing the function so that it takes a callback makes it more flexible: its user can choose whatever hashing algorithm is desired and the function will continue to work, since it uses the callback to turn option names into hashes; thus, callbacks allow the user of a function to fine-tune it at runtime. Another use is in error signaling. A Unix program, for example, might not want to terminate immediately when it receives SIGTERM; to make sure things get taken care of, it would register the cleanup function as a callback.
Callbacks may also be used to control whether a function acts or not: Xlib allows custom predicates to be specified to determine whether a program wishes to handle an event. The following code in C demonstrates the use of callbacks to display two numbers.

 #include <stdio.h>
#include <stdlib.h> /* The calling function takes a single callback as a parameter. */
void PrintTwoNumbers(int (*numberSource)(void)) {
printf("%d and %d\n", numberSource(), numberSource());
} /* A possible callback */
int overNineThousand(void) {
return (rand() % ) + ;
} /* Another possible callback. */
int meaningOfLife(void) {
return ;
} /* Here we call PrintTwoNumbers() with three different callbacks. */
int main(void) {
PrintTwoNumbers(&rand);
PrintTwoNumbers(&overNineThousand);
PrintTwoNumbers(&meaningOfLife);
return ;
}

This should provide output similar to:
 125185 and 89187225
 9084 and 9441
 42 and 42

Note how this is different from simply passing the output of the callback function to the calling function, PrintTwoNumbers() - rather than printing the same value twice, the PrintTwoNumbers calls the callback as many times as it requires. This is one of the two main advantages of callbacks.
The other advantage is that the calling function can pass whatever parameters it wishes to the called functions (not shown in the above example). This allows correct information hiding: the code that passes a callback to a calling function does not need to know the parameter values that will be passed to the function. If it only passed the return value, then the parameters would need to be exposed publicly.[examples needed]
Another example:

 /*
* This is a simple C program to demonstrate the usage of callbacks
* The callback function is in the same file as the calling code.
* The callback function can later be put into external library like
* e.g. a shared object to increase flexibility.
*
*/ #include <stdio.h>
#include <string.h>
#include <stdlib.h> typedef struct _MyMsg {
int appId;
char msgbody[];
} MyMsg; void myfunc(MyMsg *msg)
{
if (strlen(msg->msgbody) > )
printf("App Id = %d \n Msg = %s \n",msg->appId, msg->msgbody);
else
printf("App Id = %d \n Msg = No Msg\n",msg->appId);
} /*
* Prototype declaration
*/
void (*callback)(void *); int main(void)
{
MyMsg msg1;
msg1.appId = ;
strcpy(msg1.msgbody, "This is a test\n"); /*
* Assign the address of the function 'myfunc' to the function
* pointer 'callback'
*/
callback = (void *)myfunc; /*
* Call the function
*/
callback((MyMsg*)&msg1); return ;
}

The output after compilation:

$ gcc cbtest.c

$ ./a.out

App Id = 100

Msg = This is a test