"In the C language, the function itself is not a variable, but can define a pointer to the function. This pointer can be assigned, stored in an array, passed to the function and the return value as a function, etc." - "the c propming language" - "THE C Programming SECOND EDITION "
A simple example is given below to illustrate a pointer to the function.
The first example illustrates how the pointer to the function will explain, assign, and call.
#include
#define testdate 100
INT FUNC (INT A) for printing an integer * /
{
Return Printf ("% d / n", a);
}
Main ()
{
INT (* FunctionPionter) (INT A);
FunctionPionter = func;
(* Functionpionter) (TestDate);
Return 0;
}
The mean of focus statements is as follows:
INT (* FunctionPionter) (INT A);
FunctionPionter: Points to a function of a function that returns an integer, this pointer has an integer parameter.
FunctionPionter = func;
Point FunctionPionter to function func; where functions must have been defined, and the return value of the description of the function and function pointer must be consistent.
(* Functionpionter) (TestDate);
By function pointer call function; because the function pointer has pointed to the function, the contents of the function pointer are used as the function itself.
The following example shows how to pass the pointer to the function to the function as the return type of the function. In this example, there are three functions:
Hello: Returns the function of the character pointer to return strings "Hello World! / N"
Retfunc: Returns a function of a pointer to the function and returns that the function indicated by the pointer is a function that returns a character pointer.
Call: Returns a void * type pointer, and the call has a parameter of a pointer to the function, and this function pointer returns a character pointer
#include
#define max 100
Main ()
{
Void * Call (char * (*));
CHAR * (* RTNFUNC ()) ();
/ * The above instructions are some complex * /
Printf ("% s", call (RTNFUNC ()));
Return 0;
}
Char * Hello ()
{
Return "Hello World! / N";
}
Char * (* RTNFunc ()) ()
{
Return hello;
}
Void * call (char * (* func) ())
{
RETURN (* func) ();
}
In the above example, Main () cannot call the Hello function directly, and use two functions to return Hello and call Hello, respectively, and to adjust Hello in main (). Although, it seems that this program is excess but very well, how to pass the pointer to the function to the function, as a function of the function. The Call function utilizes the flexible mechanism of the VOID * type pointer, making the applicability of Calls greatly increase, which is also one of the advantages of pointers to the function. The same example is "The C Programming Language Second Edition" below this function call:
Qsort (void **) lineptr, 0, nlines-1, (int (*) (void *, void *)) (Numeric? Numcmp: strcmp));
Among them, two forced type conversions were used, and the second even utilizes a pointer to the function, the type of function is converted. Of course, the above statement cannot pass on some compilers, because some compiler requires conditions: expression 1? Expression 2: Expression 3
Expression 2 is the same as the type of expression 3. Of course, this requirement is not in line with ANSI standards. In the ANSI standard, if the expression 2 is different from the type of expression 3, the type of the result is determined by the type conversion rule. Of course, we can change the same as the type of two functions to achieve the purpose:
Qsort ((void **) LinePTR, 0, NLINES-1, NUMERIC? (int (*) (void *, void *)) Numcmp: (int (*) (void *, void *)) strcmp));
For how to directly explain a function of a pointer to the function like RTNFUNC, I have answered a lot of information, and I didn't find the answer, and I finally explored my hard scalp. Thus, I also have a more profound experience on the complex statement of C, which will be written in the later technical diary. Of course, in my opinion, too much, inappropriate use of these complex instructions, is not a good programming style, because it will make the program difficult to understand, and also increase the possibility of error.
A better compromise method is to use TypeDef to make the program's meaning clear. The following is given an example of writing the above program with typedef, defining a type PTOFUN, using typedef to indicate the pointer type pointing to the function, and the function refers to the function indicated by the pointer returns a character pointer and there is no parameter.
#include
#define max 100
Typedef char * (* ptofun) ();
Main ()
{
Void * Call (PTOFUN);
PTOFUN RTNFUNC ();
Printf ("% s", call (RTNFUNC ()));
Return 0;
}
Char * Hello ()
{
Return "Hello World! / N";
}
PTOFUN RTNFUNC ()
{
Return hello;
}
Void * call (PTOFUN FUNC)
{
RETURN (* func) ();
}
The readability of the rewritable program is greatly increased, giving people a sense of feeling.