-1

I am getting an error in my partition function that reads:

assignment makes integer from pointer without a cast 
v= array[low]; 

comparison between pointer and integer [-Werror]

These are the error I'm getting throughout the partition function. Please help.

void quickSort(void *array[], int low, int high)
{
    int p;
    if(low < high)
    {
        p= partition(*array, low, high);
        quickSort(array, low, p-1);
        quickSort(array, p+1, high);
    }
} 

int partition(void *array[], int low, int high)
{
    int v, i, j, temp;
    v= array[low];
    i=low;
    j=high+1;
    do
    {
        do 
            i++;
        while(array[i]<v && i <=high);
        do 
            j--;
        while(v<array[j]);

        if(i<j)
        {
            temp =array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }while(i<j);

    array[low] = array[high];
    array[high] = v;

    return(j);
} 
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
dylan
  • 17
  • 3

2 Answers2

1

array is an array of pointers, so array[low] is a pointer; you can't meaningfully compare this with the integer in v. You need to dereference the pointers to get to the values. And to access them as int this you need to cast the pointer type.

v = *(int*)array[low];

All the other uses of array need to be modified similarly, e.g.

while (*(int*)array[i] < v && i <= high)
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

assignment makes integer from pointer without a cast

Comes from this line v= array[low];

This is because array is an array of void pointers (i.e. array[low] is also a voidpointer) and v is an int. So looking at types you are doing

int = void*

so as the error says - you have a pointer (void pointer) and makes an int from the pointer. That is illegal.

comparison between pointer and integer [-Werror]

comes from these lines:

while(array[i]<v && i <=high);
....
while(v<array[j]);

The reason is same as described above - you mix int type with pointer type.

I'm not sure what you should do to solve but maybe the array shall be casted and dereferenced as int. Like

v= array[low];  ---->  v= *((int*)array[low]);
                            |^^^^^^^^^^^^^^|
                              cast void* to get an int*
                          |-----------------|
                           Dereference the int* to get an int

Similar for the two while statements.

That said, I see other things in your code that looks strange to me.

For instance:

array[high] = v;

I'm not sure what you are trying here but again there is a type mismatch.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63