使用C中的qsort时的警告 [英] Warning when using qsort in C

查看:1768
本文介绍了使用C中的qsort时的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写我的比较函数

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

和我有我的声明

int cmp (const int * value1,const int * value2);

和我打电话,像这样在我的计划的qsort

and I'm calling qsort in my program like so

qsort(currentCases,round,sizeof(int),cmp);

当我编译它,我得到以下警告

when i compile it I get the following warning

warning: passing argument 4 of ‘qsort’ from incompatible pointer type
/usr/include/stdlib.h:710: note: expected ‘__compar_fn_t’ but argument is of type ‘int
(*)(const int *, const int *)’

为什么不使用像IM的方式,程序工作得很好,所以我唯一担心的是什么?

The program works just fine so my only concern is why it doesn't like the way im using that?

推荐答案

CMP 函数的原型必须

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

您可以施展它的qsort(不推荐)的调用:

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

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

或注塑空隙指针为int三分球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;
   ...

这篇关于使用C中的qsort时的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆