I'm not sure if you can really avoid to maintain these 2 functions, but you can do something like this:
int cmp_int(const void *pa, const void *pb)
{
int a = *(int *)pa;
int b = *(int *)pb;
return cmp(a, b);
}
int cmp_voidp(const void *pa, const void *pb)
{
int a = *(int*)(*(void**)pa);
int b = *(int*)(*(void**)pb);
return cmp(a, b);
}
static int cmp(const int a, const int b)
{
if (a > b)
return +1;
else
if (b > a)
return -1;
else
return 0;
}
You have 3 functions, but you don't repeat yourself and it's more easy to maintain.
EDIT: Like Sergey L. said, if you're using C99
, cmp
could be a static inline
function.