C Language Study-function pointer usage
Function pointer usage
1. function pointer Definition
The first time I used the function pointer, I declared it like this,Typedef void (* pSimulatepks) (uint8 * prxBuf, uint8 * prxBufLen );
The first bracket () is used to package pSimulatepks into a pointer, and the second bracket () is a function parameter list. Therefore, pSimulatepks is a function pointer, pointer to such a type of function.
A function pointer is declared above, so that a function pointer can be defined in this way,PSimulatepks pfun;
2. function pointer call Function
For example, a function isVoid fun (uint8 * prxBuf, uint8 * prxBufLen );You can do this,Pfun = fun;
You can call fun later,Pfun (rxBuf, & rxBufLen );Or(* Pfun) (rxBuf, & rxBufLen );
3. function pointer as function return value
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 is the function name of this class function.