0

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);
2
  • 4
    Provide correct cast operators: (muchie*) Commented Jan 16, 2016 at 15:25
  • 2
    Just use std::sort, that's what it's for. Commented Jan 16, 2016 at 15:42

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, boy! Thanks a lot for pointing that out! I've been staring at the screen for a while!
@rptoma - No problem, glad to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.