Code:
/* function pointers to comparison functions */
typedef int (*DoubleCompFunc)(double, double);
typedef int (*IntCompFunc)(int, int);
/* comparison functions */
static int cmp_lt(const double a, const double b) { return a < b; }
static int cmp_le(const double a, const double b) { return a <= b; }
static int cmp_eq(const double a, const double b) { return a == b; }
static int cmp_ge(const double a, const double b) { return a >= b; }
static int cmp_gt(const double a, const double b) { return a > b; }
static int icmp_lt(const int a, const int b) { return a < b; }
static int icmp_le(const int a, const int b) { return a <= b; }
static int icmp_eq(const int a, const int b) { return a == b; }
static int icmp_ge(const int a, const int b) { return a >= b; }
static int icmp_gt(const int a, const int b) { return a > b; }
/************************/
/** depending on the relational operator stored in relop[] the function
* pointer *ptr is set to point to the appropriate comparison function
*/
static int setDoubleCompFuncPtr(char relop[], DoubleCompFunc *ptr)
{
if(strcmp(relop, "lt") == 0) *ptr = cmp_lt;
else if(strcmp(relop, "le") == 0) *ptr = cmp_le;
else if(strcmp(relop, "eq") == 0) *ptr = cmp_eq;
else if(strcmp(relop, "ge") == 0) *ptr = cmp_ge;
else if(strcmp(relop, "gt") == 0) *ptr = cmp_gt;
else
//error handling
return 1;
}
static int setIntCompFuncPtr(char relop[], IntCompFunc *ptr)
{
if(strcmp(relop, "lt") == 0) *ptr = icmp_lt;
else if(strcmp(relop, "le") == 0) *ptr = icmp_le;
else if(strcmp(relop, "eq") == 0) *ptr = icmp_eq;
else if(strcmp(relop, "ge") == 0) *ptr = icmp_ge;
else if(strcmp(relop, "gt") == 0) *ptr = icmp_gt;
else
//error handling
return 1;
}
How can I write a more generic function "setCompFuncPtr" instead
of using the two functions above which contain almost identical code?
... some void pointer action, I guess ...
I've tried the following:
Code:
int main(...)
{
...
DoubleCompFunc compFunc;
DoubleCompFunc dblfptr[] = {cmp_lt, cmp_le, cmp_eq, cmp_ge, cmp_gt};
IntCompFunc intfptr[] = {icmp_lt, icmp_le, icmp_eq, icmp_ge, icmp_gt};
...
if( (setCompFuncPtr("gt", compFunc, dblfptr)) == 0)
return -1;
...
return 0;
}
static int setCompFuncPtr(char relop[], void *ptr, void *fptr)
{
if(strcmp(relop, "lt") == 0) *ptr = fptr[0];
else if(strcmp(relop, "le") == 0) *ptr = fptr[1];
else if(strcmp(relop, "eq") == 0) *ptr = fptr[2];
else if(strcmp(relop, "ge") == 0) *ptr = fptr[3];
else if(strcmp(relop, "gt") == 0) *ptr = fptr[4];
else
//error handling
return 1;
}
But that produces lots of junk or a segmentation fault. And the compiler
throughs some warnings about dereferencing void pointers in the function
setCompFuncPtr().
What have I missed? I thought function pointers and void pointers had
both the size of 4 bytes? So, where is the problem?
[edit]
Damn! Reading the FAQ before posting is truly helpful! "You may not dereference a void*"
So, I guess, I will have to go with a simple if-else and typecast the void* appropriately ...
[/edit]
Thank you for your help!
Compiler: gcc 3.2 (cygwin)
OS: W2k
CPU: Intel x86, 32bit