Warning when using qsort in C

前端 未结 2 1656
无人共我
无人共我 2020-12-19 09:28

I wrote my comparison function

int cmp(const int * a,const int * b)
 {
   if (*a==*b)
   return 0;
else
  if (*a < *b)
    return -1;
else
    return 1;
}         


        
2条回答
  •  渐次进展
    2020-12-19 10:14

    The cmp function's prototype must be

    int cmp(const void* a, const void* b);
    

    You can either cast it in the invocation of qsort (not recommended):

    qsort(currentCases, round, sizeof(int), (int(*)(const void*,const void*))cmp);
    

    or casts the void-pointers to int-pointers in cmp (the standard approach):

    int cmp(const void* pa, const void* pb) {
       int a = *(const int*)pa;
       int b = *(const int*)pb;
       ...
    

提交回复
热议问题