Binary Insertion Sort (Generic) void pointer issues (C programming)

Hello Guys!
Im working on a Binary Insertion Sort with Generic type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    void b_insertionSort(void *a, int start, int end, const int (*comparator)(void *p1, void *p2))
{
    int i, loc, j;

    for (i = (start + 1); i <= end; ++i)
    {
        j = i - 1;
        void *selected = a[i];

        loc = binarySearch(a, selected, start, j, comparator);

        while (j >= loc)
        {
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = selected;
    }
}


I've never implemented a generic function in C and i've never used void pointers.. sorry for the all illegal expressions and other illegal stuffs.

I have several errors.. first of all

void *selected = a[i];
a value of type "void" cannot be used to initialize an entity of type "void *"C/C++(144)

Why i cant assign a void pointer, to a void pointer? In this type of algorithm i have to save the element that have to be inserted in the array after the position have been found with the binary search.

The second error is:

a[j + 1] = a[j];

expression must be a modifiable lvalueC/C++(137)

I cant assign the right value to the left (?)
I cannot figure out how to implement this algorithm.

Im literally lost. I tried different way to assign the value. But i would have to understand why i cant assign the value.. maybe because the compiler doesn't know the size? Can anyone explain to me please?

Thank you all guys in advice for the help.
Last edited on
Why i cant assign a void pointer, to a void pointer?
You're not assigning a void pointer to a void pointer. 'a' is a void pointer. a[i] is just... the void. In other words, you can't dereference a void pointer.

Your second error is the same as your first, it doesn't make sense because you can't dereference a[j + 1] to begin with.

maybe because the compiler doesn't know the size?
Yep, that's a practical reason for why you can't dereference a void pointer. There's no way to know what the size of that dereferenced object is.
Last edited on
missed that you were using C ... was going to recommend a template in c++ :)

when it comes down to it, there are only what, 3 things you can really sort on?
floating point types,
integer types
strings
even if you sort a custom type, like a struct, it will be sorted on one of the above (are you going to also allow sorting off multiple fields at once? that is a slightly bigger and more aggravating problem but perfectly doable).
maybe that will help you make the choices on how to turn the void*s back to useful things.
Last edited on
I must use C for this exercise..

I maybe found a solution: using a generic swap method to which i pass a function pointer that implement the swap based on the datatype.

What do you think?
Last edited on
Yes, the function passed in as the function pointer is what needs to convert the void* pointer into a concrete pointer, and then dereference that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Example program
#include <stdio.h>

void foo(const void* input)
{
    const int* ptr = (const int*)input;
    printf("%d\n", *ptr);
}

void bar(const void* input)
{
    const char* ptr = (const char*)input;
    printf("%s\n", ptr);
}

void call_function(const void* data, void (*func)(const void*))
{
    func(data);   
}

int main()
{
    {
        int data = 42;
        call_function(&data, foo);
    }
    
    {
        const char* str = "hello";
        call_function(str, bar);
    }
}
Last edited on
exactly. but the swap can promote. so floats can be promoted quietly to doubles and back down without needed 2 copies, and 1,2,4,8 byte ints can be promoted to 8 and back down silently, again with 1 function doing 4 types (maybe 8 types counting signed) and so on.
Ok guys, i will try with a swap function specific for the datatype. I really thank you all for the help. You have been really helpfull. THank you guys!!
Topic archived. No new replies allowed.