Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #13192

Re: qsort

From 88888 Dihedral <dihedral88888@googlemail.com>
Newsgroups comp.lang.c
Subject Re: qsort
Date 2011-11-12 10:19 -0800
Organization http://groups.google.com
Message-ID <2038935.61.1321121971518.JavaMail.geo-discussion-forums@prep8> (permalink)
References <4ebd2a87$0$19698$bbae4d71@news.suddenlink.net> <c90a242d-b4ae-4d17-bdaf-4b14b02d4e76@y42g2000yqh.googlegroups.com> <0.e94a5ba691aa7dd240d5.20111111163949GMT.874nyay5nu.fsf@bsb.me.uk>

Show all headers | View raw


On Saturday, November 12, 2011 12:39:49 AM UTC+8, Ben Bacarisse wrote:
> Malcolm McLean <malcolm...@btinternet.com> writes:
> 
> > On Nov 11, 4:00 pm, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> <snip>
> >> double prices[]={24.45,24,21};
> <snip>
> > /* in the main function */
> >
> > qsort(prices, 3, sizeof(double), compdoubles);
> 
> It's almost always better to write
> 
>   qsort(prices, 3, sizeof *prices, compdoubles);
> 
> in these cases.  When the type has been lost, you can't do this, of
> course, but if you can, I can't see any reason not to.
> 
> The gain is small since you usually can't change the array type without
> having to make other changes to the qsort call, but it still saves the
> reader a millisecond or two of checking that the size, at least, is
> right.
> 
> [In this case the 3 can also be replaced, but that it less common in
> real code.]
> 
> -- 
> Ben.

OK, this is the famous function pointer that can be reloaded in C. The qsort accepts function pointers customized. Thus, it is easy to delegate a function.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-11 09:00 -0500
  Re: qsort Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-11 06:56 -0800
    Re: qsort Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-11-11 16:39 +0000
      Re: qsort 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-12 10:19 -0800
  Re: qsort John Gordon <gordon@panix.com> - 2011-11-11 15:20 +0000
    Re: qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-11 14:22 -0500
  Re: qsort pete <pfiland@mindspring.com> - 2011-11-11 20:27 -0500
    Re: qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-12 19:10 -0500
      Re: qsort Keith Thompson <kst-u@mib.org> - 2011-11-12 16:40 -0800
        Re: qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-13 01:39 -0500
          Re: qsort Ian Collins <ian-news@hotmail.com> - 2011-11-13 19:51 +1300
            Re: qsort Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-14 02:14 +0200
            Re: qsort Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-14 00:43 -0800
      Re: qsort pete <pfiland@mindspring.com> - 2011-11-12 22:42 -0500

csiph-web