qsort 与结构数组?
qsort with array of structs?
我试图在结构数组上使用 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;
}
我试图在结构数组上使用 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;
}