Document directory
Function pointer
Today I saw a function pointer definition with several students:
Void (* F (INT, void (*) (INT)
I have seen it in C trap pit fails in the past, but the articles are very detailed, but they often make it difficult for beginners to grasp the key points and get a sewage. Here we will briefly introduce the function pointer definition method.
What is a function pointer?
This problem is well understood from the definition perspective. The pointer to a function is a function pointer, but how do we declare a function pointer? How can we forcibly convert an address to a function pointer of a certain type? The following example shows the source code:
Void function (int)
{
A = 5;
}
Void (* pfunc) (INT );
The above Code declares a function fucntion and a function pointer pfunc. It points to a function that has the void return value and the int parameter. If you give the function address to the pfunc pointer, you can simply assign values through the following two types:
Pfunc = function;
Or
Pfunc = & function;
You can call this function using pointers in either of the following ways:
Pfunc (5); or (* pfunc) (5 );
Let's take a look at the value assignment statement, pfunc = function; but sometimes it may be a constant of 0x8999940, which exactly indicates a safe function with the same function as function, how can I assign this value to pfunc? Obviously, we need to force type conversion. What type should we convert this constant? This is the key to the problem!
In the void (* pfunc) (INT) Statement, only pfunc is the variable name. The rest part, void (*) (INT), is the required conversion type. Therefore, the new value assignment statement is:
Pfunc = (void (*) (INT) 0x8999940;
After the assignment is complete, you can call the corresponding function through pfunc (5); or (* pfunc) (5.
If we understand the above content, we can explain the relatively complicated problem of void (* signal (INT, void (*) (INT ).
Returns the function name of the function pointer.
Now let's move away from the complex definition above. First, let's look at the requirements below. 1) define a function. 2) This function has the following features: two parameters, and the return value is a function pointer, and a parameter is also a function pointer. If the return value and the parameter function pointer are the same as void (*) (INT), the other function parameter is of the int type. The Function Definition name is my_func.
We can easily define this function as needed:
Typedef void (* Handler) (INT); // Parameter Function and return Function Definition
Handler my_func (INT, Handler );
Suddenly, typedef is not allowed to be used in the demand. This is because typedef is not supported in the early C language. How can this function be defined?
Let's say that the returned value of my_func is int. Can it be defined as follows:
Int my_func (INT, void (*) (INT ));
That is to say, my_func (INT, void (*) (INT) is an int type data. Replace int with a function, that is
Void (*) (INT) my_func) (INT, void (*) (INT );
This definition is obviously not supported by this syntax. How is it actually expressed? Let's look at the Declaration format of the function pointer.
Void (* pfunc) (int );
Pfunc is equivalent to void (*) (INT ). Now let's see if the above format is familiar. By the way, pfunc is my_func (INT, void (*) (INT )). If you replace the two, is it in this format:
Void (* my_func (int, void (*) (int)
If I replace my_func with signal, is it the complicated reputation we mentioned at the beginning of this article? Do you understand it now? It turns out to be a function that returns function pointers!