The so-called function pointer is a pointer to a function, that is, we define a function pointer, the space where the variable is to hold the address of a function. So what does a function pointer do besides being a parameter to a callback function? Here we combine the role of STAITC to explore how a function pointer acts as a spy.
First of all discuss the role of static, static from the nature of the two functions:
First, restricted storage domain: Static modified variables, whether local or global, will be stored in the static zone by the compiler. In fact, there is no static area concept in the generated elf format file in gcc, so the so-called static zone is a kind of general term that we summarize the data section of the program. In fact, the compiler divides the static modified variables into two categories, depending on the situation: when the variable is defined and initialized to a value other than 0, the variable is placed in the. Data segment and is placed in the. BSS segment when otherwise initialized or initialized to zero. We can summarize the two paragraphs as static areas without further discussion. While the variables placed in the static zone cause a long life cycle due to the storage domain, the length is one run of the program (which should be exactly the process after the program is running), and the normal local variables are assigned to the stack area during the run, so the life cycle is just a function call process.
Second, scoped: Because the scope of static or ordinary local variable itself is inside the function, so the scope of static is mainly to the global variables and functions of the limit. Global variables or functions that are modified by STAITC are marked by the compiler to be used only in this file. Because the compiler generates a target file that ends in. O in the compilation process as a source file that ends in. C, the last connector will not allow the address of the static modified variable or function to be linked externally during the connection process. This prevents the problem of duplicate names of global variables or functions, and prevents procedural logic problems caused by misoperation of unrelated global variables. Therefore, static limits the scope of a variable or function.
So is the static modifier really only available in this file? The answer is no, because the essence of the C language-the powerful function of the pointer makes it easy to use a function pointer to invoke a static-modified function outside the file.
Let's start with an experimental code (see Figure 1):
Figure 1
Two source files were written in code MAIN.C and GLOBAL_FUN.C.
Where MAIN.C defines a static modified function int local_fun (void);
The Global_fun () function is defined in GLOBAL_FUN.C;
Each of the two functions prints only one sentence to indicate which function you are. The compiler will make an error during connection (see Figure 2) because the static limit makes it impossible to invoke the Local_fun function in Global_fun:
Figure 2
Then modify the program, define the Global_fun function as a function with a parameter as a function pointer, and define a function pointer p in the main function to point to the static function Local_fun that is modified by static, and then call Global_ The fun function and pass the parameter p to it (see Figure 3):
Figure 3
This time the compilation runs, everything is ok (see Figure 4):
Figure 4
Summary: Pointers are the essence of the C language, and the flexible and practical use of pointers will make your development work more effortless. So mastering the hands of pointers is an essential skill for a programmer engaged in C development. And because of the flexibility of pointers, we have to be cautious when using pointers to manipulate data, which can lead to fatal program errors. But the strong C language is only a tool, as long as the understanding of the operating system is familiar with the rules of the C language and carefully use the tool, then you will feel the joy of C programming.
Original link:
Static and function pointer go