Signal function: void (* signal (INT, void (*) (INT );
The signal function is a famous signal function, but its definition is very complicated ......
Any c variable is composed of "type" + "expression", which represents the value of the "expression". The returned type is the value of the given "type", such
Int;
That is, evaluate expression A, which is an int type;
Similarly,
Int func ();
The meaning of this statement is that the result of evaluate the expression func () is an int type, that is, func is a function whose return value is an int type.
Further,
Int *;
That is, * A is an integer variable, and A is a pointer and a pointer to an integer variable;
So,
Int * func ();
Similarly, because () has a higher priority than *, func is a function that returns a pointer to an integer variable;
Further,
INT (* func )();
This * is enclosed in parentheses. Because * is executed first, func is naturally a pointer, and it points to a function of the type, that is, func is a pointer to a function, and this function returns an integer variable.
Another question to be discussed is how to get a type conversion character specified by the type, such:
Int;
Float B;
To forcibly convert B to int type
Int;
Remove the variable name in, remove the variable name at the end, and enclose the remaining parts in parentheses, namely:
(INT)
Therefore, the following expression:
INT (* func )();
If we want to get the type conversion operator of the corresponding function pointer, remove func, remove; and then use parentheses.
Include the remaining parts, namely:
(INT (*)())
Indicates a type conversion operator that points to the pointer of a function whose return value is an integer type.
So how should we convert the constant 0 to a function pointer and the return value type of this function is void?
According to the above,
(Void (*)())
This does not need to be explained, so it is easy to force type conversion:
(Void (*) () 0
Now it is a function pointer, Which is abbreviated as FP:
# Define FP (void (*) () 0
It is very easy to call this function pointer,
(* FP )()
Of course, FP is a function pointer, which can be abbreviated
FP ()
Of course, this is just short ......
Therefore, (* FP) () expands the macro FP:
Yes
(* (Void (*) () 0 )()
This complicated stuff is actually very simple. It is to convert the type of constant 0 to a function pointer pointing to the return type void first, and then
Call it again.
Use typedef to simplify (* (void (*) () 0 )():
Typedef void (* funcptr )();
(* (Funcptr) 0 )();
The Declaration of the famous signal function is like this:
Void (* signal (INT, void (*) (INT );
Similarly, typedef can simplify it:
Typedef void (* Handler) (INT );
Handler signal (INT, Handler );
This is a form we are used to seeing.