warning: assignment discards qualifiers from pointer target type

Discussion in 'C Programming' started by Jason, Oct 8, 2003.

  1. Jason

    Jason Guest

    I have a function (Inet_ntop) that returns const char * and if I try to
    assign that return value to a char * variable, I get the gcc error message:
    warning: assignment discards qualifiers from pointer target type

    Does anyone know what this warning means? Why do I get it? The program
    compiles and appears to work, but I'd like to understand the warning.

    Here is basically what the code looks like:

    char *str;
    str = Inet_ntop(...); //returns const char *

    Any help is appreciated. Thanks.

    --
    Jason
    [ jake1138 AT yahoo DOT com ]
     
    Jason, Oct 8, 2003
    #1
    1. Advertising

  2. Jason

    Mike Wahler Guest

    "Jason" <> wrote in message
    news:bm027n$uu0$...
    > I have a function (Inet_ntop) that returns const char * and if I try to
    > assign that return value to a char * variable, I get the gcc error

    message:
    > warning: assignment discards qualifiers from pointer target type
    >
    > Does anyone know what this warning means?


    It means you're walking on eggs. :)

    > Why do I get it?


    You have a good compiler.

    >The program
    > compiles and appears to work, but I'd like to understand the warning.


    The moment you try to modify what the assigned-to pointer
    points to, you could possibly induce undefined behavior.

    >
    > Here is basically what the code looks like:
    >
    > char *str;
    > str = Inet_ntop(...); //returns const char *


    It's warning you that the "protection" of the pointer's
    target provided by the 'const' qualifier is lost when
    the 'const' is thrown away by assigning it to a "plain"
    type 'char*' pointer.

    It's warning you that you're boating in deep water,
    and you've thrown your life preserver overboard.

    const char array[]="whatever";
    array[0] = 'X'; /* compiler must tell you, "Can't do that!" */

    const char *cp = array;
    cp[0] = 'X'; /* compiler must tell you, "Can't do that!" */


    char *p = array; /* valid, but many compilers will warn you:
    "Danger, Will Robinson!" */

    /* because... */

    p[0]; /* is valid, although possibly/probably not what you
    really wanted, and has possiblity of undefined behavior */


    > Any help is appreciated. Thanks.


    Write:

    const char *str;
    str = Inet_ntop(...); //returns const char *


    The function returns type 'const char*' for a reason.
    It's telling you "You can look, but don't touch the
    target of the returned pointer."

    HTH,
    -Mike
     
    Mike Wahler, Oct 8, 2003
    #2
    1. Advertising

  3. Jason

    Mike Wahler Guest

    "Jason" <> wrote in message
    news:bm027n$uu0$...
    > I have a function (Inet_ntop) that returns const char * and if I try to
    > assign that return value to a char * variable, I get the gcc error

    message:
    > warning: assignment discards qualifiers from pointer target type
    >
    > Does anyone know what this warning means?


    It means you're walking on eggs. :)

    > Why do I get it?


    You have a good compiler.

    >The program
    > compiles and appears to work, but I'd like to understand the warning.


    The moment you try to modify what the assigned-to pointer
    points to, you could possibly induce undefined behavior.

    >
    > Here is basically what the code looks like:
    >
    > char *str;
    > str = Inet_ntop(...); //returns const char *


    It's warning you that the "protection" of the pointer's
    target provided by the 'const' qualifier is lost when
    the 'const' is thrown away by assigning it to a "plain"
    type 'char*' pointer.

    It's warning you that you're boating in deep water,
    and you've thrown your life preserver overboard.

    const char array[]="whatever";
    array[0] = 'X'; /* compiler must tell you, "Can't do that!" */

    const char *cp = array;
    cp[0] = 'X'; /* compiler must tell you, "Can't do that!" */


    char *p = array; /* valid, but many compilers will warn you:
    "Danger, Will Robinson!" */

    /* because... */

    p[0]; /* is valid, although possibly/probably not what you
    really wanted, and has possiblity of undefined behavior */


    > Any help is appreciated. Thanks.


    Write:

    const char *str;
    str = Inet_ntop(...); //returns const char *


    The function returns type 'const char*' for a reason.
    It's telling you "You can look, but don't touch the
    target of the returned pointer." If you ignore this
    warning, you're on your own.

    HTH,
    -Mike
     
    Mike Wahler, Oct 8, 2003
    #3
  4. Jason

    James Hu Guest

    On 2003-10-08, Jason <> wrote:
    >
    > char *str;
    > str = Inet_ntop(...); //returns const char *
    >


    You should declare str variable to be the same type as that being
    returned by Inet_ntop().

    const char *str = Inet_ntop(/* ... */);

    The "const" is a qualifier. It means the return value of Inet_ntop
    is a pointer to data that should not be modified.

    However, in the erroneous code, you assigned that pointer to a
    regular "char *", which discards the "should not be modified"
    qualifier.

    -- James
     
    James Hu, Oct 8, 2003
    #4
  5. Jason

    Jason Guest

    Thanks Mike and James for your answers! I think I was confused as to how
    const worked with pointers in that way, but now I understand.

    --
    Jason
    [ jake1138 AT yahoo DOT com ]
     
    Jason, Oct 9, 2003
    #5
  6. Jason

    Mike Wahler Guest

    "Jason" <> wrote in message
    news:bm2j8v$fe$...
    > Thanks Mike and James for your answers! I think I was confused as to how
    > const worked with pointers in that way, but now I understand.


    It can get confusing. :)

    /* (0) */ int * pi; /* pointer to int */
    /* (can modify p or *p) */

    /* (1) */ int const * pci; /* pointer to const int */
    /* (can modify p, but not *p) */

    /* (2) */ const int * pci2; /* same as (1) */

    /* (3) */ int * const cpi; /* const pointer to int */
    /* (can modify *p, but not p */

    /* (4) */ int const * const cpci; /* const pointer to const int */
    /* (cannot modify p nor *p) */

    /* (5) */ const int * const cpci2; /* same as (4) */


    HTH,
    -Mike
     
    Mike Wahler, Oct 9, 2003
    #6
  7. Jason

    Mike Wahler Guest

    Re: (corr) warning: assignment discards qualifiers from pointer target type

    "Mike Wahler" <> wrote in message
    news:Z_hhb.4350$...

    > /* (4) */ int const * const cpci; /* const pointer to const int */
    > /* (cannot modify p nor *p) */
    >
    > /* (5) */ const int * const cpci2; /* same as (4) */


    Dang, that's what I get for 'copy-n-pasting' :)

    Of course the 'p' and '*p' in the comments refer to
    the actual identifier in each declaration
    ('pci', 'cpi', etc.)

    Sorry about that.

    -Mike
     
    Mike Wahler, Oct 9, 2003
    #7
    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. Charlie Zender
    Replies:
    12
    Views:
    1,127
    Peter Pichler
    Jan 3, 2004
  2. Pietro Cerutti

    return discards qualifiers from pointer target type

    Pietro Cerutti, Aug 28, 2007, in forum: C Programming
    Replies:
    17
    Views:
    1,050
    Barry Schwarz
    Sep 1, 2007
  3. Thelma Lubkin
    Replies:
    2
    Views:
    542
  4. Andre
    Replies:
    4
    Views:
    24,917
    Andrey Tarasevich
    Mar 4, 2008
  5. Greg Hauptmann
    Replies:
    0
    Views:
    315
    Greg Hauptmann
    Feb 6, 2009
Loading...

Share This Page