qsort 与结构数组? [英] qsort with array of structs?

查看:35
本文介绍了qsort 与结构数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在结构数组上使用 qsort 但我收到此错误:*"标记之前的预期主表达式

I am trying to use qsort on an array of structs but I get this error: expected primary-expression before '*' token

struct muchie {
    int x,y,c;
} a[100];

int cmp(const void* p, const void* q)
{
    muchie vp,vq;
    vp=*(muchie* p);
    vq=*(muchie* q);
    return vp.c-vq.c;
}

// ....

qsort(a,m,sizeof(muchie),cmp);

推荐答案

参数的转换错误 - 应该是 *(muchie*)p 而不是 *(muchie* p).

The casting of the parameters is wrong - should be *(muchie*)p instead of *(muchie* p).

使用:

int cmp(const void* p, const void* q)
{
    muchie vp,vq;
    vp=*(muchie*) p;
    vq=*(muchie*) q;
    return vp.c-vq.c;
}

这篇关于qsort 与结构数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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