Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Need warning-free qsort() comparison functions

Reply
Thread Tools

Need warning-free qsort() comparison functions

 
 
Severian
Guest
Posts: n/a
 
      01-02-2004
On 01 Jan 2004 14:29:27 -0800, Ben Pfaff <(E-Mail Removed)> wrote:

>Charlie Zender <(E-Mail Removed)> writes:
>
>> int /* O [enm] Comparison result [<,=,>] 0 iff val_1 [<,==,>] val_2 */
>> nco_cmp_int /* [fnc] Compare two integers */
>> (const void *val_1, /* I [nbr] Number to compare */
>> const void *val_2) /* I [nbr] Number to compare */
>> {
>> /* Purpose: Compare two integers
>> Function is suitable for argument to ANSI C qsort() routine in
>> stdlib.h
>> Code based on responses to my comp.lang.c thread 20040101 */
>> const int * const val_1_ip=val_1;
>> const int * const val_2_ip=val_2;
>> return (*val_1_ip > *val_2_ip) - (*val_1_ip < *val_2_ip);
>> } /* end nco_cmp_int() */

>
>Do you really find code with that comment style readable? It
>looks pretty bad to me.


Pretty much gave me a headache too.

I would prefer:

int compints(const void *p1, const void *p2)
{
const int *pi1, *pi2;
return *pi1 - *pi2;
}

Of course if you're convinced that the things being compared may not
really be signed ints, or would result in overflow, you could use an
actual comparison, as Goodsell recommended.

Wherefore any comments? If someone can't understand what that code is
doing, they have no reason to be looking at it!

- Sev

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-02-2004
Severian <(E-Mail Removed)> writes:

> I would prefer:
>
> int compints(const void *p1, const void *p2)
> {
> const int *pi1, *pi2;
> return *pi1 - *pi2;
> }


I'd prefer to see pi1 and pi2 initialized
--
Just another C hacker.
 
Reply With Quote
 
 
 
 
Al Bowers
Guest
Posts: n/a
 
      01-02-2004


Severian wrote:


>
> I would prefer:
>
> int compints(const void *p1, const void *p2)
> {
> const int *pi1, *pi2;
> return *pi1 - *pi2;
> }
>
> Of course if you're convinced that the things being compared may not
> really be signed ints, or would result in overflow, you could use an
> actual comparison, as Goodsell recommended.
>
> Wherefore any comments?


As written, function compints is flawed.
ITYM
const int *pi1 = p1, *pi2 = p2;

--
Al Bowers
Tampa, Fl USA
mailto: http://www.velocityreviews.com/forums/(E-Mail Removed) (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
Severian
Guest
Posts: n/a
 
      01-02-2004
On 01 Jan 2004 16:23:39 -0800, Ben Pfaff <(E-Mail Removed)> wrote:

>Severian <(E-Mail Removed)> writes:
>
>> I would prefer:
>>
>> int compints(const void *p1, const void *p2)
>> {
>> const int *pi1, *pi2;
>> return *pi1 - *pi2;
>> }

>
>I'd prefer to see pi1 and pi2 initialized


D'oh. I'll blame it on my new year's hangover!

 
Reply With Quote
 
Maurizio Loreti
Guest
Posts: n/a
 
      01-02-2004
CBFalconer <(E-Mail Removed)> writes:

> int
> compare_ints(const void *val_1,const void *val_2)
> {
> const int *i1p = val_1, i2p = val_2;


I think you mean "const int *i1p = val_1, *i2p = val_2;", don't you?

--
Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy ROT13: (E-Mail Removed)
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-02-2004
Maurizio Loreti wrote:
> CBFalconer <(E-Mail Removed)> writes:
>
> > int
> > compare_ints(const void *val_1,const void *val_2)
> > {
> > const int *i1p = val_1, i2p = val_2;

>
> I think you mean "const int *i1p = val_1, *i2p = val_2;", don't you?


It's all the fault of this contrary keyboard. Never trust
them, verify everything.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Charlie Zender
Guest
Posts: n/a
 
      01-02-2004
Thanks, I like your return statement best and will adopt it.

Charlie

> return *i1<*i2?-1*i1>*i2);


--
Charlie Zender, (E-Mail Removed), (949) 824-2987, Department of Earth
System Science, University of California, Irvine CA 92697-3100
Visiting NCAR 12/13/03--1/17/04: ***********************************
Voice/FAX: (303) 497-1724/1348, Office: Mesa Lab 259b **************


 
Reply With Quote
 
Charlie Zender
Guest
Posts: n/a
 
      01-02-2004

> Do you really find code with that comment style readable?


Yes

> It looks pretty bad to me.


That is probably because it is very dense.
With good syntax highlighting (e.g., Emacs), you get used to it.
I realize it is non-standard but it works for me for scientific programming.
Without documenting the input/output characteristics of the arguments,
and their "dimensional units", I would soon be lost.

Charlie
--
Charlie Zender, (E-Mail Removed), (949) 824-2987, Department of Earth
System Science, University of California, Irvine CA 92697-3100
Visiting NCAR 12/13/03--1/17/04: ***********************************
Voice/FAX: (303) 497-1724/1348, Office: Mesa Lab 259b **************


 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      01-02-2004

On Fri, 2 Jan 2004, Charlie Zender wrote:
>
> Thanks, I like your return statement best and will adopt it.


But please please PLEASE add some whitespace to it!

> > return *i1<*i2?-1*i1>*i2);


return (*i1 < *i2)? -1: (*i1 > *i2);

-Arthur
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-03-2004
CBFalconer wrote:

> Maurizio Loreti wrote:
>> CBFalconer <(E-Mail Removed)> writes:
>>
>> > int
>> > compare_ints(const void *val_1,const void *val_2)
>> > {
>> > const int *i1p = val_1, i2p = val_2;

>>
>> I think you mean "const int *i1p = val_1, *i2p = val_2;", don't you?

>
> It's all the fault of this contrary keyboard. Never trust
> them, verify everything.


This is truer than it should be. My keyboard (of which I am very fond, and
about which I will not hear a word raised in anger) occasionally takes it
upon itself to insert random characters in the stream. For example, when I
indulge in that very bad habit of travelling long distances up with the
cursor keys in insert mode (instead of typing, say, ESC39k, as God
intended), it will slide a surreptitious 8 into the text. Downwards? 2,
naturally. You may even have seen examples of this in my Usenet articles on
occasion.

Also, occasionally, the shift key gets stuck, and needs an additional tap to
unstick it. Perversely, this doesn't matter so much in insert mode, since I
CAN AT LEast see that it's happened, so I can correct for it. But when I'm
in command mode, it can get a bit - um, thank heaven for ESC:q!

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
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
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
Date Comparison and Manipulation Functions? W. eWatson Python 17 08-30-2008 05:37 PM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Comparison of functions Steven D'Aprano Python 29 08-01-2005 05:47 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments