带有结构数组的 qsort

qsort with array of structs?

本文关键字:qsort 数组 结构      更新时间:2023-10-16

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

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)

用:

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