Generic Sort



Hello All,

I'm trying to replicate a general purpose sort function (think qsort)

void sort(void *arr, const int num, size_t size,
int (*cmp)(void *a, void *b))
{
int i = 0 ;
int j = 0 ;

for (i = (num - 1) ; i >= 0 ; i--)
{
for (j = 1 ; j <= i ; j++)
{
if(cmp((int *) &arr[j-1], (int *) &arr[j])) //Error
{
//swapping logic
}
}
}
}

MSVC 8 (2005) reports 2 errors when trying to call cmp:-

error C2036: 'void *' : unknown size
error C2036: 'void *' : unknown size

I've tried a few variations, but I can't seem to get it right. Can
anybody help?

Thanks in advance.

.



Relevant Pages

  • Re: Generic Sort
    ... I'm trying to replicate a general purpose sort function (think qsort) ... void sort(void *arr, const int num, size_t size, ...
    (comp.lang.c)
  • Re: Generic Sort
    ... void sort(void *arr, const int num, size_t size, ...
    (comp.lang.c)
  • Re: casts
    ... int compbigramfreq(const void *vp1, const void *vp2) ... That's how qsort works. ... because the children who designed this language never even thought ... tool because of its apparent lack of safety and use of void pointers, ...
    (comp.lang.c)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... You can't do pointer arithmetic on a void* value. ... qsort behaves in a manner consistent with its specification. ... actually works as advertised...but doesn't mean that the cast is ...
    (comp.lang.c)
  • Re: Pointer initialization.
    ... >When you pass a pointer to qsort, ... and convert it to one of these 1-megabyte-wide "void *"s. ... and use those to compare the desired "zorg"s. ...
    (comp.lang.c)