Reprinted please indicate the source
Author: Pony
When I first learned C, I didn't pay much attention to function pointers. Later I started embedded software programming. With the accumulation of knowledge, I learned deeply, only then can we find that the role of function pointers in the field of embedded software is so great (other fields are not very clear ). to emphasize the function pointer, we have to mention the object-oriented idea. The concept of Object-oriented has been dominant in software development since its birth. Its powerful functions, in-depth analysis of the software itself, Object-based ideas, and so on, all of which have revolutionalized the methods for software developers to solve problems. because of this, the object-oriented thinking can become popular in all fields of software development, and even has a place in the mainstream embedded software field of C and assembly. For example, analyticdb provides C ++-supported compilers. in Microsoft's open source wince code, we can also see many drivers written in C ++.Program.
However, C ++ also has its drawbacks. its functions and memory collection mechanisms make it cost less efficient than C. this tiny difference may be negligible in other software development fields, but in the field of embedded software, manyCodeIt runs on chips with limited memory space and clock speed, so there are strict requirements on the code size and running speed.
Although the object-oriented language is not available, the idea of object-oriented can be used for reference. The most important concept in object-oriented thinking is class. in some embedded software applications, if this idea is applied, you suddenly find that your code has become "advanced. for example, the author uses the structure and function pointer of C to implement the "class ". similar to the following format
Typedef struct menu
{
Uint8 xposition; // X coordinate of the menu
Uint8 yposition; // y coordinate of the menu
Void (* Action) (void); // The Action triggered when the menu is pressed.
} Menu
Every menu on the LCD is treated as an object. In this way, you only need to assign values to the corresponding struct. This completely subverts the old-fashioned way of judging with if or switch. The idea of class is too powerful here.
The following describes the function pointer. Assume that I write an event handler in the following form:
Void buffer (void) // The parameter type and return value type are determined according to the pointer function declaration.
{
.....
}
I want to call the above function when pressing a menu. I can assign this action to the "member function" of menu in the following form"
Menu;
Menu. Action = (void (*) () buffer;
With the above concept, we can analyze a complex function pointer. In embedded programming, sometimes we need to let the program jump to a specific address for execution. If it is implemented in C, we can use the following form.
(* (Void (*) () 0 )()
It looks complicated. if you split it for analysis, it will be clear at a glance.
First, 0 is the address of the program to be executed. then void (*) () is a function pointer type. it is like int * is a pointer type pointing to the int type. however, this function pointer type also declares the return type and parameter type of the function. the last step is to call this function. when using a function pointer to call a function, this form is similar to (* p. here P is equivalent
(Void (*) () 0.