What is the difference between pointer functions and function pointers
1. These two concepts are referred to as, the pointer function is a function of the pointer, that is, the essence is a function. We know that the function returns a type (if not returned, it is valid), but the pointer function return type is a certain type of pointer. Its definition format is as follows:
Return Type Identifier * Return Name (Form Parameter Table)
{Function body}
The return type can be any basic type and composite type. The use of functions returned to the pointer is very wide. In fact, every function, even if it does not bring a type of pointer, it has an entry address, which is equivalent to a pointer. For example, the function returns a whole value, which is actually equivalent to the value of a pointer variable, but the variable at this time is the function itself, and the entire function is equivalent to a "variable". For example, an example of a returning pointer function below:
#include
FLOAT * FIND ();
Main ()
{
Static float score [] [4] = {{60, 70, 80, 90}, {56, 89, 34, 45}, {34, 23, 56, 45}
Float * p;
INT I, M;
Printf ("Enter the Number to Be Found:");
Scanf ("% D", & M);
Printf ("" The score of no.% d are: / n ", m);
P = Find (score, m);
For (i = 0; i <4; i )
Printf ("% 5.2F / t", * (p i));
}
FLOAT * FIND (Float (* Pionter) [4], INT N) / * Defines the pointer function * /
{
Float * pt;
Pt = * (Pionter N);
Return (PT);
}
The student number is calculated from the 0 number, and the function Find () is defined as a pointer function, and the Pointer Pointer is a pointer variable to the one-dimensional array of four elements. Pointer 1 points to the first line of Score. * (Pointer 1) points to the 0th element of the first line. PT is a pointer variable that points to floating point variables. The file () function is called in the main () function, pass the first address of the Score array to the Pointer.
2, "Function Pointer" is a pointer variable to the function, so "function pointer" itself should first be a pointer variable, but the pointer variable points to functions. This is just like the pointer variables can point to integer variables, characters, arrays, here is a pointing function. As mentioned earlier, C is compiled, each function has an inlet address, the entrance address is the address pointed to by the function pointer. With the pointer variables of the function, you can use the pointer variable call function, just like the same type variable with the pointer variable, which is consistent. The function pointer has two purposes: call the function and the parameters of the function. Description of the function pointer is:
Data type flag (* Pointer variable name) (parameter); Note: The parameters in the function parentheses can have no, depending on the situation.
The following program illustrates the method of function pointer modification function:
#include
INT MAX (INT X, INT Y) {RETURN (X> Y? x: Y);
void main ()
{
INT (* PTR) ();
INT A, B, C;
PTR = max;
Scanf ("% D,% D", & A, & b);
C = (* PTR) (A, B);
Printf ("A =% D, B =% D, MAX =% D", A, B, C);
}