Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qsort gives [Error] : invalid conversion from `int (*)(cricketer*, cricketer*)' to `int (*)(const void*, const void*)'

Tags:

c

qsort

This is the code, it sorts the data of cricketers by avg runs. The qsort function is showing errors:

[Error] C:\Users\Encoder\Documents\C-Free\Temp\Untitled3.cpp:29: error: invalid conversion from int (*)(cricketer*, cricketer*) to int (*)(const void*, const void*)

[Error] C:\Users\Encoder\Documents\C-Free\Temp\Untitled3.cpp:29: error: initializing argument 4 of `void qsort(void*, size_t, size_t, int ()(const void, const void*))'

include

#include<stdlib.h>
struct cricketer  //structure for details of cricketer
{
    int avg_run;
    char name[20];
    int age;
    int match_no;
} c[4];
int sort(struct cricketer *a, struct cricketer *b);   //pre-defining sort function 
int main()     //main function
{
    int i, s;
    for (i = 0; i < 3; i++)    //enumerating structure records.
    {
        printf("enter the name of cricketer ");
        fflush(stdin);
        gets(c[i].name);
        printf("enter his age,his no of matches and total average runs ");
        scanf("%d%d%d",&c[i].age, &c[i].match_no, &c[i].avg_run);
        }
    printf("records before sorting");  
    for (i = 0; i < 3; i++)
    {
        printf("\n\nname ");
        puts(c[i].name);
        printf("age %d\nno of matches %d\naverage runs %d\n",c[i].age, c[i].match_no, c[i].avg_run);
    }
    qsort(c, 3, sizeof(c[0]), sort); //sorting using qsort
    printf("\nrecords after sorting");
    for (i = 0; i < 3; i++)
    {
        printf("\n\nname ");
        puts(c[i].name);
        printf("age %d\nno of matches %d\naverage runs %d\n",c[i].age, c[i].match_no, c[i].avg_run);
    }
}
int sort(struct cricketer *a, struct cricketer *b)  //sort function definition
{
    if (a->avg_run == b->avg_run)
        return 0;
    else 
        if ( a->avg_run > b->avg_run)
            return 1;
        else 
            return -1;
}
like image 272
s_anzer Avatar asked Nov 26 '25 21:11

s_anzer


1 Answers

The function whose pointer you pass to qsort must be

int sort(const void* va, const void* vb);

Because that's what qsort expects. Then within that function you have to do at the beginning

const struct cricketer *a = (struct cricketer*) va;
const struct cricketer *b = (struct cricketer*) vb;

Or if you prefer accessing with dots . instead of arrows ->

const struct cricketer a = *(struct cricketer*) va;
const struct cricketer b = *(struct cricketer*) vb;

See an example at this reference

Regarding the error message, this int (*)(cricketer*, cricketer*) is a pointer to a function that gets 2 pointers to cricketer as arguments. The compiler expects such a function pointer int (*)(const void, const void*) and it's telling you it cannot convert the former to the latter. Also note how you need pointer to const data as sort is not supposed to modify the data.

like image 140
Manos Nikolaidis Avatar answered Nov 29 '25 14:11

Manos Nikolaidis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!