Suppress 'qualifiers discarding' warning for strstr()

Discussion in 'C Programming' started by lovecreatesbea...@gmail.com, Nov 20, 2007.

  1. Guest

    I'm getting `warning: return discards qualifiers from pointer target
    type' at line 11 on this code from gcc.

    Some people suggested in old posts that this kind of warning can be
    suppressed by qualifying the return type with const. In this code it
    also can be removed by removing const from the first pointer parameter
    str. Are there better ways to do it, or does it deserve to do this?


    /* Search str for sub and return the pointer to the beginning of the
    first occurence, or return null for no match. */

    char *strstr(const char *str, const char *sub)
    {
    int i, j;

    for (i = 0; str; i++)
    {
    for (j = 0; sub[j]; j++)
    if (str[i + j] != sub[j])
    break;
    if (!sub[j])
    return &str; /*line 11*/
    }
    return 0;
    }

    Thank you for your time.
    , Nov 20, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > I'm getting `warning: return discards qualifiers from pointer target
    > type' at line 11 on this code from gcc.
    >

    Fix the code, th waning is a good one.

    > Some people suggested in old posts that this kind of warning can be
    > suppressed by qualifying the return type with const. In this code it
    > also can be removed by removing const from the first pointer parameter
    > str. Are there better ways to do it, or does it deserve to do this?
    >

    The return type should be const, it is a pointer to a cont object.
    >
    > /* Search str for sub and return the pointer to the beginning of the
    > first occurence, or return null for no match. */
    >
    > char *strstr(const char *str, const char *sub)
    > {
    > int i, j;
    >
    > for (i = 0; str; i++)
    > {
    > for (j = 0; sub[j]; j++)
    > if (str[i + j] != sub[j])
    > break;
    > if (!sub[j])
    > return &str; /*line 11*/


    str is const char*, so is &str.

    --
    Ian Collins.
    Ian Collins, Nov 20, 2007
    #2
    1. Advertising

  3. Eric Sosman Guest

    Ian Collins wrote On 11/20/07 13:33,:
    > wrote:
    >
    >>I'm getting `warning: return discards qualifiers from pointer target
    >>type' at line 11 on this code from gcc.
    >>

    >
    > Fix the code, th waning is a good one.
    >
    >
    >>Some people suggested in old posts that this kind of warning can be
    >>suppressed by qualifying the return type with const. In this code it
    >>also can be removed by removing const from the first pointer parameter
    >>str. Are there better ways to do it, or does it deserve to do this?
    >>

    >
    > The return type should be const, it is a pointer to a cont object.


    If the code is supposed to work the same way as the
    Standard library's strstr(), that's not a solution.

    >>/* Search str for sub and return the pointer to the beginning of the
    >>first occurence, or return null for no match. */
    >>
    >>char *strstr(const char *str, const char *sub)
    >>{
    >> int i, j;
    >>
    >> for (i = 0; str; i++)
    >> {
    >> for (j = 0; sub[j]; j++)
    >> if (str[i + j] != sub[j])
    >> break;
    >> if (!sub[j])
    >> return &str; /*line 11*/

    >
    >
    > str is const char*, so is &str.


    ... and the answer is to convert it from `const char*'
    to plain `char*' with a cast:

    return (char*)&str;
    or
    return (char*)str + i;

    .... or other equivalents. This is one of those places where
    C's type-matching rules are a hindrance rather than a help,
    and you have to be rude to them to get them to shut up and
    leave you alone.

    --
    Eric Sosman, Nov 20, 2007
    #3
  4. "" <> wrote in message
    > I'm getting `warning: return discards qualifiers from pointer target
    > type' at line 11 on this code from gcc.
    >
    > /* Search str for sub and return the pointer to the beginning of the
    > first occurence, or return null for no match. */
    >
    > char *strstr(const char *str, const char *sub)
    >

    The problem is that const has been tacked onto the language instead of being
    incorporated from the initial design.
    Some standard library functions, including strstr(), simply cannot be
    written sensibly with the const paradigm. The problem is that strstr has got
    to know whether the parameters are "really const" or not, and then return
    either a const pointer or a plain pointer. There is no way of expressing
    that, so all you cna do is return a plain pointer and cast the constness
    away, thus losing the protection.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Nov 28, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    6
    Views:
    8,981
    Mike Wahler
    Oct 9, 2003
  2. Thelma Lubkin
    Replies:
    2
    Views:
    514
  3. Thelma Roslyn Lubkin
    Replies:
    1
    Views:
    348
    Erik Wikström
    Dec 28, 2007
  4. Simon Strandgaard
    Replies:
    0
    Views:
    105
    Simon Strandgaard
    Feb 23, 2004
  5. Ralph Shnelvar
    Replies:
    1
    Views:
    87
    Brian Candler
    Aug 5, 2010
Loading...

Share This Page