Mikroelektronika MIKROE-738 Datenbogen
206
mikoC PRO for PIC32
MikroElektronika
Function Pointers
Function Pointers are pointers, i.e. variables, which point to the address of a function.
// Define a function pointer
int (*pt2Function) (float, char, char);
Note: Thus functions and function pointers with different calling convention (argument order, arguments type or
return type is different) are incompatible with each other.
Assign an address to a Function Pointer
It’s quite easy to assign the address of a function to a function pointer. Simply take the name of a suitable and known
function. Using the address operator & infront of the function’s name is optional.
//Assign an address to the function pointer
int DoIt (float a, char b, char c){ return a+b+c; }
pt2Function = &DoIt; // assignment
Example:
int addC(char x,char y){
return x+y;
}
int subC(char x,char y){
return x-y;
}
int mulC(char x,char y){
return x*y;
}
int divC(char x,char y){
return x/y;
}
int modC(char x,char y){
return x%y;
}