Can we call functions using function pointer? if yes how?
我们可以使用函数指针调用函数吗?如果有,怎么样?
4 个解决方案
#1
13
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.
}
#3
2
Yes you can.
是的你可以。
#4
1
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); ...
#1
13
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.
}
#2
#3
2
Yes you can.
是的你可以。
#4
1
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); ...