473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1618
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: richard | last post by:
What might cause qsort to crash? I'm using qsort to sort a few thousand items of a not too complex structure. Tried it with one data set - worked fine. Tried another similarly sized data set...
11
by: William Buch | last post by:
I have a strange problem. The code isn't written by me, but uses the qsort function in stdlib. ALWAYS, the fourth time through, the memory location of variable list (i.e. mem location = 41813698)...
7
by: Angus Comber | last post by:
Hello Here is my code so far. Is this correct/incorrect/along the right lines/other? #include <stdio.h> #include <string.h> #include <search.h> struct mystruct {
7
by: Excluded_Middle | last post by:
Suppose I have a struct typdef struct foo { int age; char *name; }foo; now I made a list of foo using
17
by: Trent Buck | last post by:
The fourth argument is a comparator that returns `an integer less than, equal to, or greater than zero' depending on the ordering of its arguments. If I don't care about the order and simply...
32
by: John Smith | last post by:
I'm trying to figure out qsort(). I haven't seen any practical examples, only synopsis. In the code below, the array is not sorted. Can someone give me some help? #include <stdio.h> #include...
16
by: t_pantel | last post by:
I 've got the following structure: typedef struct GROUPED { short val ; short code; short group; short forecast_cd; short double_ind; short min;
10
by: No Such Luck | last post by:
Hi All: The code below (using the qsort function) produces the following incorrect result. The last two numbers are not sorted. It this innaccurate result specific to my compiler's qsort, or is...
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
10
by: gauss010 | last post by:
Suppose I have an object A of type char. Each A is a buffer containing a string, and I want to sort the M strings of A using the strcmp function. The description of the qsort function says that I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.