Toma Radu-Petrescu
Toma Radu-Petrescu

Reputation: 2262

qsort with array of structs?

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);

Upvotes: 0

Views: 1033

Answers (1)

Danny_ds
Danny_ds

Reputation: 11406

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;
}

Upvotes: 1

Related Questions