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


Groups > comp.lang.c > #23650

Re: qsort man page

From pacman@kosh.dhis.org (Alan Curry)
Newsgroups comp.lang.c
Subject Re: qsort man page
Date 2012-07-24 04:29 +0000
Organization Aioe.org NNTP Server
Message-ID <jul8ap$7i2$1@speranza.aioe.org> (permalink)
References <89644623-7e1c-4f8b-9f4d-6443526f0f73@u2g2000yqj.googlegroups.com> <500C31F3.518B@mindspring.com> <0.1f04de2e214d1e935a0b.20120722213623BST.87k3xv5y9k.fsf@bsb.me.uk> <500E0B1B.653C@mindspring.com>

Show all headers | View raw


In article <500E0B1B.653C@mindspring.com>,
pete  <pfiland@mindspring.com> wrote:
>
>I don't know.
>Maybe I am being wrongly influenced by the warnings
>generated by the compiler that I am using.
>
>When I rewrite and compile cmpstringp to do the type conversions 
>through assignment to intermediate objects onstead of by casts, 
>I get warnings when the intermediate objects are of the same type 
>as the cast from the original version of cmpstringp, (char *const *).

That's strange. The assignments aren't any worse than the cast. I think
they're slightly better because they're more constrained (the cast will
convert any pointer to any other pointer; the assignment requires compatible
types or void).

>
>I don't get warnings when the intermediate objects are 
>of type (const char **).
>
>
>/* BEGIN new.c */
>
>#include <string.h>
>
>int
>cmpstringp(const void *p1, const void *p2)
>{
>    return strcmp(* (char * const *) p1, * (char * const *) p2);
>}
>
>int
>cmpstringp_2(const void *p1, const void *p2)
>{
>    /*
>    ** This function is OK
>    */
>    const char **pp1;
>    const char **pp2;
>
>    pp1 = p1;
>    pp2 = p2;

gcc warns here.
new.c: In function 'cmpstringp_2':
new.c:20: warning: assignment discards qualifiers from pointer target type
new.c:21: warning: assignment discards qualifiers from pointer target type

It didn't even need -Wall to produce those warnings. They're pretty severe.
And they're right. After the assignments, *pp1 = "oops" would be allowed
according to the type of pp1, but it's pointing to something that the caller
of the function didn't give you permission to modify.

>
>    return strcmp(*pp1, *pp2);
>}
>
>int
>cmpstringp_3(const void *p1, const void *p2)
>{
>    /*
>    ** This function generates warnings
>    */
>    char *const *pp1;
>    char *const *pp2;
>
>    pp1 = p1;
>    pp2 = p2;
>    /*
>    ** (35) : warning C4090: '=' : different 'const' qualifiers
>    ** (36) : warning C4090: '=' : different 'const' qualifiers
>    */
>    return strcmp(*pp1, *pp2);
>}
>
>/* END new.c */

gcc doesn't find anything wrong with cmpstringp_3 even with more warnings
enabled. Could you try your compiler with typedefs for the inner pointer?

typedef char *string;
typedef const char *fixedstring;

int
cmpstringp_4(const void *p1, const void *p2)
{
    fixedstring *pp1;
    fixedstring *pp2;

    pp1 = p1;
    pp2 = p2;

    return strcmp(*pp1, *pp2);
}

int
cmpstringp_5(const void *p1, const void *p2)
{
    const string *pp1;
    const string *pp2;

    pp1 = p1;
    pp2 = p2;
    return strcmp(*pp1, *pp2);
}

cmpstringp_4 and cmpstringp_5 should be exactly equivalent to cmpstringp_2
and cmpstringp_3 respectively, unless I messed up. gcc treats them the same.
Warnings on cmpstringp_4, none on cmpstringp_5.

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


Thread

qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-21 14:14 -0700
  Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-21 22:57 +0100
    Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-21 18:07 -0700
      Re: qsort man page Mark Storkamp <mstorkamp@yahoo.com> - 2012-07-23 08:49 -0500
        Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-23 10:44 -0700
          Re: qsort man page tom st denis <tom@iahu.ca> - 2012-07-26 09:02 -0700
            Re: qsort man page John Gordon <gordon@panix.com> - 2012-07-26 16:41 +0000
              Re: qsort man page tom st denis <tom@iahu.ca> - 2012-07-26 09:59 -0700
      Re: qsort man page Ike Naar <ike@sverige.freeshell.org> - 2012-07-23 14:43 +0000
        Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-23 13:16 -0700
          Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-23 13:29 -0700
            Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-23 16:56 -0700
              Re: qsort man page Noob <root@localhost> - 2012-07-24 09:29 +0200
                Re: qsort man page Keith Thompson <kst-u@mib.org> - 2012-07-24 23:17 -0700
              Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-25 09:51 -0700
                Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-25 20:51 -0700
                Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-26 14:27 -0700
                Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-26 18:20 -0700
                Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-28 07:24 -0700
        Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-23 23:39 -0400
    Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-22 00:11 -0400
      Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-22 14:41 +0100
        Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-22 13:01 -0400
          Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-22 21:36 +0100
            Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-23 22:40 -0400
              Re: qsort man page pacman@kosh.dhis.org (Alan Curry) - 2012-07-24 04:29 +0000
                Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-24 11:40 +0100
                Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-24 22:06 -0400
        Re: qsort man page Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 11:38 -0700
  Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-23 23:34 -0400
    Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-24 00:07 -0400
  Re: qsort man page "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-07-24 16:36 -0700

csiph-web