The pointer function generally refers to the function that returns the pointer;
#include <stdio.h>
int* fun (int *a)
{return
A;
}
int main (int argc, char **argv)
{
int a = 3;
printf ("%d", * (Fun (&a));
return 0;
}
A function pointer is a pointer to the start address of a function:
The first step is to understand the call procedure for a function:
#include <stdio.h>
int fun (int i)
{return
i + 1;
}
int main (int argc, char **argv)
{
int r;
r = Fun (5);
R = (*fun) (5); Call Way
printf ("%d\n", r);
return 0;
}
A function can be invoked with R = (*fun) (5), which means that the function name is actually a pointer,
Addressed by (*fun). So we can define a pointer
#include <stdio.h>
int fun (int i)
{return
i + 1;
}
int main (int argc, char **argv)
{
int r;
Int (*FUNP) (int); Declaration pointer
//FUNP = fun; Assign values to pointers
FUNP = &fun;
r = FUNP (5);
printf ("%d\n", r);
return 0;
}
Therefore, there are two ways of assigning value to function pointers.
Similarly, there are two ways to call a function through a function pointer:
#include <stdio.h>
int fun (int i)
{return
i + 1;
}
int main (int argc, char **argv)
{
int r;
Int (*FUNP) (int); Declaration pointer
FUNP = fun; Assigning value to the pointer
//r = FUNP (5);
R = (*FUNP) (5); Invoke
printf ("%d\n", r);
return 0;
}
That is, the fun () and (*fun) () are the same except where the declaration is made.
In this way, the C language is also easy to implement a structure similar to a callback function:
#include <stdio.h>
int funa (int i)
{return
i + 1;
}
int Funb (int i)
{return
i-1;
}
void Fun (int (*FUNP) (int), int i)
{
printf ("%d\n", FUNP (i));
}
int main (int argc, char **argv)
{
int (*FUNP) (int); Declaration pointer
FUNP = Funa; Assigning value to the pointer
//FUNP = FUNB; Assign value to
the pointer Fun (FUNP, 5); Call return
0;
}
in the fun () function, it does just at some point call a FUNP pointer to the function, which function, in the definition of the fun function is not known, until the Funa assigned to function pointers FUNP,FUNP specifically what function to do to determine.
that is, the main function determines what function code the FUN function needs to help it implement, but fun when and whether to call the function that main gave him, which is determined by fun ().