Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the callback function of C language

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use the callback function of C language". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the callback function of C language.

The application of pointer is the essence of C language programming, and callback function is the advanced application of function pointer in C language. In short, a callback function is a function called through a function pointer. If you pass a function pointer (the entry address of a function) to another function, when the function pointer is used to call the function it points to, we say that the function is a callback function.

Why use callback functions? Let's first look at a small example:

Node * Search_List (Node * node, const int value) {while (node! = NULL) {if (node-> value = = value) {break;} node = node-> next;} return node;}

This function is used to find a specified value in an one-way linked list and return the node where the value is saved. Its parameters are pointers to the nodes of the linked list and the values to look for. This function looks very simple, but let's consider one problem: it can only be applied to linked lists whose values are integers. If we look up a string linked list, we have to write another function. In fact, most of the code is the same as this function, except that the type of the second parameter and the method of comparison are different.

In fact, we prefer to make the lookup function independent of type, so that it can be used to find a linked list that holds values of any type, so we have to change the way of comparison, and this can be achieved with the help of callback functions. We write a function (callback function) to compare two values of the same type, and then pass a pointer to the function as an argument to the lookup function, which calls the comparison function to perform the comparison. Any type of worth can be compared.

We must also pass the lookup function a pointer to the value to be compared rather than the value itself, that is, a formal parameter of type void *, which will be passed to the callback function for the final comparison. This modification allows us to pass pointers of any type to the lookup function, thus completing the comparison of any type, which is the advantage of pointers. We cannot pass strings, arrays, or structures to functions as arguments, but pointers to them can.

Now, our lookup function can be implemented like this:

NODE * Search_List (NODE * node, int (* compare) (void const *, void const *), void const * desired_value); {while (node! = NULL) {if (compare ((node- > value_address), desired_value) = 0) {break;} node = node- > next;} return node;}

As you can see, the user passes a function pointer to the lookup function, which calls back the function.

Notice here that our linked list node is defined as follows:

Typedef struct list {void * value_address; struct list * next;} NODE

This definition allows the pointer of the NODE * type to point to the linked list node that stores any type of data. Value_address is a pointer to specific data. We define it as void *, indicating a pointer to an unknown type, so that the linked list can store any type of data, and the * parameters we pass to the lookup function Search_List can be uniformly expressed as: NODE *, otherwise, we still have to write lookup functions separately to adapt to the linked lists that store different data types.

Now, the lookup function is type-independent because it doesn't actually compare, so we have to write comparison functions for different types, which is easy to implement because the caller knows the types of values contained in the linked list. if you create several linked lists that contain different type values, writing a comparison function for each type allows a single lookup function to act on all types of linked lists.

The following is a comparison function for finding in an integer linked list:

Note that the parameters of the comparison function must be declared as void * to match the prototype of the lookup function, and then cast to the (int *) type for comparing integers.

Int int_compare (void const * a, void const * b) {if (* (int *) a = = * (int *) b) {return 0;} else {return-1;}}

This function can be used as follows:

Desired_node = Search_List (root, int_compare, & desired_int_value)

If you want to look in a list of strings, the following code does the job:

Desired_node = Search_List (root, strcmp, "abcdefg")

The library function strcmp performs exactly the same comparison as we need, but gcc issues a warning: because the parameter to strcmp is declared as const char * instead of void const *.

The above example shows the basic principles and usage of callback functions, which are widely used. Usually, when we want to implement different things through a unified interface, it is very appropriate to use callback functions to implement them.

Any time you write a function that must be able to perform different types of work at different times or perform work that can only be defined by the function caller, you can use a callback function. Many window systems use callback functions to connect multiple actions, such as dragging the mouse and clicking buttons to specify the call to a specific function in the user's program.

At this point, I believe you have a deeper understanding of "how to use the callback function of C language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report