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

丶灬走出姿态 提交于 2020-12-26 08:02:53

问题


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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/33689257/qsort-gives-error-invalid-conversion-from-int-cricketer-cricketer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!