Can we call functions using function pointer? if yes how?
Agreed +1. Whew, wish they were all that easy.
TheSoftwareJedi
2008-10-31 04:14:56
int (*a)(int q){ return q+1;}does this definition work?
2008-10-31 05:19:38
@ramu: no, it does not work.
Jonathan Leffler
2008-11-01 00:44:57
+12
A:
Yes. Trivial example:
// Functions that will be executed via pointer.
int add(int i, int j) { return i+j; }
int subtract(int i, int j) {return i-j; }
// Enum selects one of the functions
typedef enum {
ADD,
SUBTRACT
} OP;
// Calculate the sum or difference of two ints.
int math(int i, int j, OP op)
{
int (*func)(int i, int j); // Function pointer.
// Set the function pointer based on the specified operation.
switch (op)
{
case ADD: func = add; break;
case SUBTRACT: func = subtract; break;
default:
// Handle error
}
return (*func)(i, j); // Call the selected function.
}
Adam Liss
2008-10-31 04:16:48
Greg: thanks for the highlighting edit -- learn something new every day!
Adam Liss
2008-10-31 04:41:14
+1
A:
Yes. An example:
Before code...
typedef int ( _stdcall *FilterTypeTranslatorType ) ( int TypeOfImportRecord, PMAType *PMA ); FilterTypeTranslatorType FilterTypeTranslator = {NULL};
Now in the code...
PMAType *PMA; HANDLE hFilterDll; // assume DLL loaded // Now find the address... ... FilterTypeTranslator[TheGroup] = ( FilterTypeTranslatorType ) GetProcAddress( hFilterDll, "FilterTypeTranslator" ); ... // now call it FilterTypeTranslator(1,PMA); ...
David L Morris
2008-10-31 04:18:52