In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to implement function members and callback functions in C language structures". The explanation in this article is simple and clear and easy to learn and understand. let's study and learn how to implement function members and callback functions in C language structures.
Often when we write a program, for example, we define an "object" (note that this does not refer to the concept of objects in high-level languages such as Clearing, recording, Java, etc.). For example, if an object is defined in C language for display initialization, an internal member variable int width is defined, which represents the width of the display in the current system, and the other variable is int height, which represents the height of the display in the current system. In addition to these two regular variables, when we initialize the monitor, we call the function "ScreenInit (intwidth, int height);".
For the above applications, if it is a simple single display, there is no problem at all, but when we need to transplant this program, each display may have different initialization functions in addition to different width and height. For this kind of application, we hope that this initialization function is also included in the whole "object", so that during the migration, we can not only define the length and width of the display, but also rewrite the initialization function of the display. that is, it is very convenient to port this display driver to the past.
In addition to the above application, for example, when we write a framework program for an embedded chip, we can define each new task as a structure that contains the scheduling time and priority of the task. Stack size, and specific task code.
In the above two applications, in addition to defining the ordinary member variables of the structure, we also need to define a very important element in the structure, that is, function. We have already made it clear that functions are not supported in the structure of C language, so can we find a way to embed the members of a function in the structure of C language? The answer is obviously yes, because although it is impossible to define a function directly inside the C language structure, the member inside the C language structure can clearly be a pointer. As we said earlier, C language pointers can point to anything with an address.
Therefore, the idea of implementing function members in C language structures becomes very clear, that is:
Define a pointer to a function inside the C language structure
When initializing a specific "object", point the pointer to the function in the structure to the function that implements the function.
It is called when this function needs to be called.
The above is about how to implement function members in C language structures, as shown in figure 1.
Fig. 1 implementation of functions in C language structure
In addition, the above principle first points the pointer to the function to the specific implementation function, and then calls it uniformly later, which is the callback function that we often talk about in the C language. There are many applications of callback functions. For example, when we are doing MCU interrupts, we can write the SCM interrupt handling function as a pointer to the function. Every time the MCU program initializes, point the function pointer inside the interrupt to the specific interrupt handling function. When the interrupt is generated, the MCU can call the pointer pointing to the function. While the program is running, we can also modify the object pointed to by the pointer to the function at any time to make the program more flexible.
Next, let's write some code to illustrate the implementation of function members in C language structures. Here is an example, write a program to calculate the perimeter or area of a rectangle according to the input, assuming that the number entered by the keyboard is 1, calculate the perimeter of the rectangle, and assume that the number entered by the keyboard is 0. Calculate the area of the rectangle.
The above program can be done in countless ways, and since today we are mainly talking about structures and callback functions, we will use these two knowledge points to write the program.
The first thing to do is to define a structure for this rectangle that can store all its related variables. let's think about it, this structure is used to describe a rectangle, and the two most critical information of a rectangle is its length and width. therefore, these two information must be included in this structure. Then, our requirement is to calculate the perimeter and area of the rectangle, so we can also define these two variables in the structure, of course, if we don't want to define it in the structure. Then we need to find the area and perimeter of the rectangle at the same time, so we can define a pointer to the function for the structure and modify the function pointed to by the pointer according to different options. Finally, according to the input needs, modify the function pointed to by the pointer to the function in this structure, and then call it. So you can write the program shown in figure 2.
Fig. 2 Application of embedded function members in the structure
Thank you for your reading, the above is the content of "how to implement function members and callback functions in C language structure". After the study of this article, I believe you have a deeper understanding of how to achieve function members and callback functions in C language structure, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.