Overview In many cases, especially when reading code written by others, the ability to understand C language statements becomes very important, the concise and concise C language also makes the statement of C language very confusing. Therefore, here I will focus on this issue with an article. Problem: Declaration and Function A program is stored in a memory segment with the starting address 0. If we want to call this program, what should we do? Answer The answer is (* (void (*) () 0 )(). It looks really big. Well, let's look at the problem in two different ways to analyze it in detail. Answer analysis: from end to end First, the most basic function declaration: void function (paramlist ); The most basic function call: function (paramlist ); Given that the function in question does not have parameters, function calling can be simplified to function (); Secondly, according to the Problem description, we can know that 0 is the entry address of the function, that is, 0 is the pointer of a function. The function declaration form using the function pointer is: void (* pfunction) (), the corresponding call form is: (* pfunction )(), the function call in the question can be written as follows: (* 0 )(). Third, we know that the function pointer variable cannot be a constant, so the 0 in the above formula must be converted to a function pointer. Let's take a look at the prototype of function pointer variables for functions using function pointers, such as void (* pfunction? This problem is very simple. The pfunction function pointer prototype is (void (*) (), that is, remove the variable name. For clarity, add the () number. Therefore, convert 0 to void. The function pointer with the null parameter is as follows: (void (*)()). OK, combined with the analysis of 2) and 3), the result is: (* (void (*) () 0 )(). Answer analysis: Understanding the answer from start to end (Void (*) () is a function pointer prototype with a return value of void and a null parameter. (Void (*) () 0. Convert 0 to a function pointer with the return value void and null parameter. The Pointer Points to 0. * (Void (*) () 0. Add * above to indicate the name of a function whose return value is void. (* (Void (*) () 0) (), which is of course a function. We can use typedef to clearly declare the following: Typedef void (* pfun )(); After this definition, pfun is a function pointer variable with a return type of void and no parameters. In this way, the function is changed to (* (pfun) 0 )(); Problem: analysis of the three statements The most fundamental method for analyzing statements is the analogy replacement method. The analogy, simplification, and understanding are made from the most basic statements. The following three examples are analyzed, to illustrate how to use this method. #1: int * (* A [5]) (INT, char *); First, we can see that the identifier a, "[]" has a higher priority than "*", and a and "[5]" are combined first. Therefore, a is an array with five elements. Each element is a pointer pointing to "(INT, char *)". Obviously, it points to a function, this function parameter is "int, char *", and the return value is "int *". OK. :) #2: void (* B [10]) (void (*)()); B is an array with 10 elements. Each element is a pointer pointing to a function. The function parameter is "void (*) ()" [Note 10 ], the return value is "Void ". Finished! Note: this parameter is a pointer pointing to a function. The function parameter is null and the return value is "Void ". #3. Doube (*) (* pA) [9]; Pa is a pointer pointing to an array with nine elements. Each element is "Doube (*) ()" (that is, a function pointer pointing to a function, the parameter of this function is null, and the returned value is "double "). Function pointer in C Language A function has a physical location in the memory, which can be assigned to a pointer. The address of a zero-point function is the entry point of the function. Therefore, function pointers can be used to call a function. The function address is obtained by using a function name without any parentheses or parameters. (This is similar to getting an array address, that is, getting an array address only when there is an array name and no subscript .) How to describe a function pointer variable? To show that the type of a variable fn_pointer is "function pointer with int returned value", you can use the following statement: INT (* fn_pointer )(); * Fn_pointer must be enclosed in parentheses to allow the compiler to correctly interpret this statement. If this pair of parentheses is missing, then: Int * fn_pointer (); The meaning is completely different. Fn_pointer is a function name, and its return value is a pointer of the int type. Function pointer variable In the C language, a function always occupies a contiguous memory zone, and the function name is the first address of the memory zone occupied by the function. We can assign the first address (or entry address) of the function to a pointer variable so that the pointer variable points to the function. Then, the pointer variable can be used to locate and call this function. We call this pointer variable pointing to a function "function pointer variable ". Function pointer variables are defined as follows: Type specifier (* pointer variable name )(); The "type specifier" indicates the type of the returned value of the function. "(* Pointer variable name)" indicates that "*" is followed by a defined pointer variable. The final empty parentheses indicate that the pointer variable refers to a function. Example: int (* PF )(); PF is a pointer variable pointing to the function entry. The return value (function value) of this function is an integer. The following example shows how to call a function using a pointer. Int max (int A, int B ){ If (A> B) return; Else return B; } Main (){ Int max (int A, int B ); INT (* Pmax )(); Int x, y, z; Pmax = max; Printf ("input two numbers:/N "); Scanf ("% d", & X, & Y ); Z = (* Pmax) (x, y ); Printf ("maxmum = % d", Z ); } The procedure for calling a function in the form of a function pointer variable is as follows: 1. Define the function pointer variable first, for example, the second line of INT (* Pmax) () in the next program. Define Pmax as the function pointer variable. 2. Assign the entry address (function name) of the called function to the function pointer variable, for example, 11th rows of Pmax = max in the program; 3. call a function in the form of a function pointer variable, for example, the program's 14th line z = (* Pmax) (x, y); the call function is generally in the form of: (* pointer variable name) when using function pointer variables, pay attention to the following two points: A. function pointer variables cannot be used for arithmetic operations, which is different from array pointer variables. Adding or subtracting an integer from an array pointer variable can move the pointer to an array element behind or before it, and moving the function pointer is meaningless. B. In function calls, the brackets on both sides of "(* pointer variable name)" are indispensable. * should not be understood as a value calculation, where * is just a symbol. |