Use of function pointers
1. function pointer definition
The first time I use a function pointer, I declare this,typedef void (*PSIMULATEPKS) (Uint8 *prxbuf, uint8 *prxbuflen);
The first parenthesis (), the PSIMULATEPKS is wrapped into a pointer, the second bracket (), meaning the argument list of a function, so Psimulatepks is a function pointer to a pointer to such a class of functions.
The above declaration of a function pointer, you can define a function pointer,PSIMULATEPKS pfun;
2. function pointer Call function
For example, there is a function that is void fun (Uint8 *prxbuf, uint8 *prxbuflen); You can do this,pfun = fun;
This can be done later when the fun is called,pfun (Rxbuf, &rxbuflen) , or (*pfun) (Rxbuf, &rxbuflen);
3. Function pointers as function return values
Psimulate simfunc (uint8 cmd)
{
Switch (CMD)
{
Case 0X01:
return sim_a;
Case 0X02:
return sim_b;
Case 0X03:
return sim_c;
Case 0X04:
return sim_d;
}
}
Among them, Sim_a Sim_b sim_c sim_d are the function names of such functions.
C Language Study-use of function pointers