468,257 Members | 1,425 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

Re: qsort problem

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);
}
Jun 27 '08 #1
3 1457
is*********@gmail.com wrote:
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 *.
Jun 27 '08 #2
Ioannis Vranos <iv*****@nospam.no.spamfreemail.grwrites:
is*********@gmail.com wrote:
>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 *.
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?

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #3
On Sun, 13 Apr 2008 05:10:15 -0700 (PDT), is*********@gmail.com wrote:
>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
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

34 posts views Thread by richard | last post: by
11 posts views Thread by William Buch | last post: by
7 posts views Thread by Excluded_Middle | last post: by
17 posts views Thread by Trent Buck | last post: by
32 posts views Thread by John Smith | last post: by
16 posts views Thread by t_pantel | last post: by
10 posts views Thread by No Such Luck | last post: by
5 posts views Thread by Bidule | last post: by
10 posts views Thread by gauss010 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.