问题
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);
回答1:
The casting of the parameters is wrong - should be *(muchie*)p
instead of *(muchie* p)
.
Use:
int cmp(const void* p, const void* q)
{
muchie vp,vq;
vp=*(muchie*) p;
vq=*(muchie*) q;
return vp.c-vq.c;
}
来源:https://stackoverflow.com/questions/34828650/qsort-with-array-of-structs