qsort中的void指针算术

时间:2014-10-12 03:37:57

标签: c++ c

我试图制作像qsort这样的东西但是当我尝试使用void*

时总是会出错
void my_sort(void* base, size_t num, size_t size, int(*compare)(const void*, const void*))
{
    size_t i, j;
    const void *first, *second;
    for (size_t i = 0; i < num*size; i+=size)
    {
        for (size_t j = size +i; j < num*size; j +=size)
        {
            first = base + i;
            second = base + j;
            if (compare(first,second)<0)
            {
                swap(base,i,j);

            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

first = ((char*) base) + i;
second = ((char*)base) + j;