Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > qsort problem

Reply
Thread Tools

qsort problem

 
 
istillshine@gmail.com
Guest
Posts: n/a
 
      04-13-2008
On Apr 13, 8:31 am, Harald van Dk <(E-Mail Removed)> wrote:
> On Sun, 13 Apr 2008 05:10:15 -0700, istillshine 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?

>
> 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?
>
> > }


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);
}
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      04-13-2008
Ioannis Vranos <(E-Mail Removed)> writes:
> http://www.velocityreviews.com/forums/(E-Mail Removed) 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      04-13-2008
On Sun, 13 Apr 2008 05:10:15 -0700 (PDT), (E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with qsort - Visual Studio Igal C Programming 0 05-21-2008 05:30 PM
Problem by sorting array of structures using qsort zavnobih@gmail.com C Programming 6 03-21-2007 02:07 AM
bsearch/qsort problem for effective_dated search non-exact c_programmer C Programming 3 06-09-2005 12:04 PM
Problem using qsort(). Please help! t_pantel@yahoo.com C Programming 16 12-10-2004 05:37 AM
qsort + structure problem Excluded_Middle C Programming 7 11-04-2004 10:31 PM



Advertisments