qsort problem

I

istillshine

I called qsort (from standard library) somewhere in my function.

qsort(temp, tree_num, sizeof(int), compare);

I defined compare before the caller in the same .c file.

int compare(const int *a, const int *b)
{
int val1, val2;

val1 = *a;
val2 = *b;

return (val1 > val2) ? -1 : ((val1 < val2) ? 1 : 0);
}


When compiling, I got this warning:

passing arg 4 of `qsort' from incompatible pointer type

I really could not find out why compare is incompatible.
 
I

Ioannis Vranos

I called qsort (from standard library) somewhere in my function.

qsort(temp, tree_num, sizeof(int), compare);

I defined compare before the caller in the same .c file.

int compare(const int *a, const int *b)
{
int val1, val2;

val1 = *a;
val2 = *b;

return (val1 > val2) ? -1 : ((val1 < val2) ? 1 : 0);
}


When compiling, I got this warning:

passing arg 4 of `qsort' from incompatible pointer type

I really could not find out why compare is incompatible.


It should be:

int compare(const void *a, const void *b);
 
I

istillshine

It should be:

int compare(const void *a, const void *b);

But this causes another warning:

dereferencing a void pointer on the line where "val1 = *a; " appears.
 
I

Ioannis Vranos

But this causes another warning:

dereferencing a void pointer on the line where "val1 = *a; " appears.


int compare(const void *a, const void *b)
{
int *pval1= a, *val2= b;

/* ... */
}
 
I

Ioannis Vranos

Correction:



int compare(const void *a, const void *b)
{
const int *pval1= a, *pval2= b;>
/* ... */
}
 
I

Ioannis Vranos

Correction, removed a typo.


Ioannis said:
Correction:


Ioannis Vranos wrote:

int compare(const void *a, const void *b)
{
const int *pval1= a, *pval2= b;
 
B

Barry Schwarz

I called qsort (from standard library) somewhere in my function.

qsort(temp, tree_num, sizeof(int), compare);

I defined compare before the caller in the same .c file.

int compare(const int *a, const int *b)
{
int val1, val2;

val1 = *a;
val2 = *b;

return (val1 > val2) ? -1 : ((val1 < val2) ? 1 : 0);
}


When compiling, I got this warning:

passing arg 4 of `qsort' from incompatible pointer type

I really could not find out why compare is incompatible.

Look in your C reference. What is the only acceptable type for the
fourth argument to qsort? What is the type of your fourth argument?
Are they the same?


Remove del for email
 
I

istillshine

I wrote a compare function for qsort. I tested version 1 and version
2 (below). Version 1 worked but version 2 did not work. Why?

/*
* Version 1
* Compare two numbers, used in qsort.
*/
int compare(const void *a, const void *b)
{
int *val1, *val2;

val1 = (int *) a;
val2 = (int *) b;

return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
}


/*
* Version 2
* Compare two numbers, used in qsort.
*/
int compare(const void *a, const void *b)
{
int *val1 = (int *) a, *val2 = (int *) b;

return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
}
 
H

Harald van Dijk

I wrote a compare function for qsort. I tested version 1 and version 2
(below). Version 1 worked but version 2 did not work. Why?

Because either your compiler is broken, or you didn't post the real code.
The two versions you posted are equivalent in every way that matters to
qsort.

That said,
int compare(const void *a, const void *b)

So the parameters are pointers to a read-only type...
{
int *val1, *val2;

val1 = (int *) a;
val2 = (int *) b;

....which you cast to pointers to a writeable type...
return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);

....in order to only read the values. Why?
 
I

Ioannis Vranos

I wrote a compare function for qsort. I tested version 1 and version
2 (below). Version 1 worked but version 2 did not work. Why?

/*
* Version 1
* Compare two numbers, used in qsort.
*/
int compare(const void *a, const void *b)
{
int *val1, *val2;

val1 = (int *) a;
val2 = (int *) b;

return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
}


/*
* Version 2
* Compare two numbers, used in qsort.
*/
int compare(const void *a, const void *b)
{
int *val1 = (int *) a, *val2 = (int *) b;

return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
}


In both versions the function arguments are const void * and you assign
them to int *, instead of const int *.
 
I

istillshine

Because either your compiler is broken, or you didn't post the real code.
The two versions you posted are equivalent in every way that matters to
qsort.

That said,


So the parameters are pointers to a read-only type...



...which you cast to pointers to a writeable type...


...in order to only read the values. Why?

Thanks. I rewrote it, as shown below.


/*
* Compare two numbers, used in qsort.
*/
int compare(const void *a, const void *b)
{
const int *val1 = a, *val2 = b;

return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
}
 
K

Keith Thompson

Ioannis Vranos said:
In both versions the function arguments are const void * and you assign
them to int *, instead of const int *.

Yes, and that should be fixed, but that's not going to cause any real
problems in the behavior of qsort(). (Also, the casts aren't
necessary.) As Harald pointed out, both versions appear to be
equivalent.

Also, the OP merely told us that version 2 "did not work". That's not
enough information. *How* did it not work?
 
B

Barry Schwarz

I wrote a compare function for qsort. I tested version 1 and version
2 (below). Version 1 worked but version 2 did not work. Why?

/*
* Version 1
* Compare two numbers, used in qsort.
*/
int compare(const void *a, const void *b)
{
int *val1, *val2;

val1 = (int *) a;
val2 = (int *) b;

return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
}


/*
* Version 2
* Compare two numbers, used in qsort.
*/
int compare(const void *a, const void *b)
{
int *val1 = (int *) a, *val2 = (int *) b;

return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
}

Since there are no significant differences between the two and neither
has an obvious error, you need to provide us the calling function and
the data you used to test. It would also be good if you told us in
what way version 2 did not work.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Qsort() messing with my entire Code 0
Qsort() is messing with my entire Code!!! 0
qsort returning index 2
qsort man page 23
qsort semantics 10
qsort 32
How to apply qsort() with a parameter? 17
calling qsort 59

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top