2

i have this struct:

typedef struct arvDado Arv; 
struct arvDado{
    char c;
    int qtd;
    Arv* dir;
    Arv* esq;
};

and i am making a array of pointer of this struct :

Arv** vetArv = (Arv**)malloc(sizeof(Arv*)*qtd);

i want to make a qsort but i think that my comper function is not working...

comper function:
int comparaCelula(const void *x, const void *y){
  Arv *a=(Arv*)x, *b=(Arv*)y;
  printf(" %d x %d",a->qtd,b->qtd);
  if(a->qtd == b->qtd) return 0;
  if(a->qtd < b->qtd) return -1;
  if(a->qtd > b->qtd) return 1;
}

for any case with you want to see my implementation of qsort is this:

array of point of Arv = Arv** vetCell / size of vet = qtd / size of struct Arv / comper function

qsort(vetCel, qtd, sizeof(Arv*), comparaCelula);
2
  • in what way is it not working? what input/output do you get? Commented Nov 20, 2015 at 0:19
  • You allocate an array of Arv*. I don't see you initializing your array anywhere. Commented Nov 20, 2015 at 0:21

1 Answer 1

4

The compare function for qsort is given the ADDRESS of the elements in the array. Since your array is an array of pointers, you're getting pointers to pointers. So, this line:

Arv *a=(Arv*)x, *b=(Arv*)y;

should be:

Arv *a=*((Arv**)x), *b=*((Arv**)y);

since x and y are pointers to pointers to Arv (Arv**)

Sign up to request clarification or add additional context in comments.

Comments

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.