warning: initialization discards qualifiers from pointer target type

Discussion in 'C Programming' started by Andre, Mar 3, 2008.

  1. Andre

    Andre Guest

    Hi All,

    When I compile the following piece of code with gcc, I get 3 "warning:
    initialization discards qualifiers from pointer target type" messages
    which refer to the 3 lines marked in the code. What's wrong with
    these lines? I searched the web for it, but didn't find anything
    useful.

    Thanks,

    Andre


    enum CompressionType {
    DEFLATE = 0,
    GZIP
    };

    struct CompressionInfo {
    char * Name;
    int32_t Type;
    uint32_t WindowSize;
    };

    static struct CompressionInfo compression_table[] = {
    { "deflate", DEFLATE, DEFLATE_WINDOW_SIZE }, <-- HERE
    { "gzip", GZIP,
    24 }, <-- HERE
    { "", -1,
    0 } <-- HERE
    };
     
    Andre, Mar 3, 2008
    #1
    1. Advertising

  2. Andre

    Michael Mair Guest

    Re: warning: initialization discards qualifiers from pointer targettype

    Andre wrote:
    > Hi All,
    >
    > When I compile the following piece of code with gcc, I get 3 "warning:
    > initialization discards qualifiers from pointer target type" messages
    > which refer to the 3 lines marked in the code. What's wrong with
    > these lines? I searched the web for it, but didn't find anything
    > useful.
    >
    > Thanks,
    >
    > Andre
    >
    >
    > enum CompressionType {
    > DEFLATE = 0,
    > GZIP
    > };
    >
    > struct CompressionInfo {
    > char * Name;
    > int32_t Type;
    > uint32_t WindowSize;
    > };
    >
    > static struct CompressionInfo compression_table[] = {
    > { "deflate", DEFLATE, DEFLATE_WINDOW_SIZE }, <-- HERE
    > { "gzip", GZIP,
    > 24 }, <-- HERE
    > { "", -1,
    > 0 } <-- HERE
    > };


    Please post actual code (ideally a minimal example) and maybe
    your gcc invocation.
    The below does not show any problems.

    root@omexochitl ~/test/C
    $ cat discard.c
    #include <stdint.h>

    #ifndef UINT32_MAX
    # error missing uint32_t
    #endif

    #ifndef INT32_MAX
    # error missing int32_t
    #endif

    #define DEFLATE_WINDOW_SIZE 0

    enum CompressionType {
    DEFLATE = 0,
    GZIP
    };

    struct CompressionInfo {
    char * Name;
    int32_t Type;
    uint32_t WindowSize;
    };

    static struct CompressionInfo compression_table[] = {
    { "deflate", DEFLATE, DEFLATE_WINDOW_SIZE },
    { "gzip", GZIP, 24 },
    { "", -1, 0 }
    };

    root@omexochitl ~/test/C
    $ gcc -std=c99 -pedantic -Wall -Wextra -c discard.c
    discard.c:24: warning: 'compression_table' defined but not used

    BTW: Using an enum "sometimes" is not exactly the most clever thing
    to do. I'd rather use
    enum CompressionType {
    INVALID = -1, /* or UNDEFINED or INITIAL or whatever */
    DEFLATE,
    GZIP
    };


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Mar 3, 2008
    #2
    1. Advertising

  3. Andre

    Eric Sosman Guest

    Re: warning: initialization discards qualifiers from pointer targettype

    Andre wrote:
    > Hi All,
    >
    > When I compile the following piece of code with gcc, I get 3 "warning:
    > initialization discards qualifiers from pointer target type" messages
    > which refer to the 3 lines marked in the code. What's wrong with
    > these lines? I searched the web for it, but didn't find anything
    > useful.
    >
    > Thanks,
    >
    > Andre
    >
    >
    > enum CompressionType {
    > DEFLATE = 0,
    > GZIP
    > };
    >
    > struct CompressionInfo {
    > char * Name;
    > int32_t Type;
    > uint32_t WindowSize;
    > };
    >
    > static struct CompressionInfo compression_table[] = {
    > { "deflate", DEFLATE, DEFLATE_WINDOW_SIZE }, <-- HERE
    > { "gzip", GZIP,
    > 24 }, <-- HERE
    > { "", -1,
    > 0 } <-- HERE
    > };


    You are probably using a compiler flag that tells gcc to
    give string literal constants the type `const char*' instead
    of `char*'. (That is, you've told gcc to compile "almost C"
    instead of C.) Either get rid of the compiler flag, or change
    your Name element to a `const char*'.

    --
     
    Eric Sosman, Mar 3, 2008
    #3
  4. Andre

    Micah Cowan Guest

    Eric Sosman <> writes:

    > Andre wrote:
    >> Hi All,
    >>
    >> When I compile the following piece of code with gcc, I get 3 "warning:
    >> initialization discards qualifiers from pointer target type" messages
    >> which refer to the 3 lines marked in the code. What's wrong with
    >> these lines? I searched the web for it, but didn't find anything
    >> useful.


    <snip>

    >> static struct CompressionInfo compression_table[] = {
    >> { "deflate", DEFLATE, DEFLATE_WINDOW_SIZE }, <-- HERE
    >> { "gzip", GZIP,
    >> 24 }, <-- HERE
    >> { "", -1,
    >> 0 } <-- HERE
    >> };

    >
    > You are probably using a compiler flag that tells gcc to
    > give string literal constants the type `const char*' instead
    > of `char*'. (That is, you've told gcc to compile "almost C"
    > instead of C.) Either get rid of the compiler flag, or change
    > your Name element to a `const char*'.


    Alternatively, he's compiling it with g++, but neglected to mention it
    since it's "essentially C code."

    Whether it's C or C++ code, probably changing the field to a const
    char * is the way to go, to prevent you from accidentally attempting
    to modify these (potentially) unmodifiable strings.

    --
    Micah J. Cowan
    Programmer, musician, typesetting enthusiast, gamer...
    http://micah.cowan.name/
     
    Micah Cowan, Mar 4, 2008
    #4
  5. Re: warning: initialization discards qualifiers from pointer targettype

    Andre wrote:
    > ...
    > When I compile the following piece of code with gcc, I get 3 "warning:
    > initialization discards qualifiers from pointer target type" messages
    > which refer to the 3 lines marked in the code. What's wrong with
    > these lines? I searched the web for it, but didn't find anything
    > useful.
    > ...


    If you are really compiling your code as C code (as opposed to C++ code), the
    warnings your are getting from your compiler are misleading and formally
    incorrect. While it is not a good idea to initialize a 'char*' pointer (as
    opposed to 'const char*' pointer) with a string literal, it still does not
    discard any qualifiers, as the warning message seems to suggest, since string
    literals in C don't have const-qualified type.

    In this case a warning from the compiler might be welcome, but of course it is
    preferable to see a more precise description of the problem.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Mar 4, 2008
    #5
    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:
    9,012
    Mike Wahler
    Oct 9, 2003
  2. Pietro Cerutti

    return discards qualifiers from pointer target type

    Pietro Cerutti, Aug 28, 2007, in forum: C Programming
    Replies:
    17
    Views:
    1,025
    Barry Schwarz
    Sep 1, 2007
  3. Thelma Lubkin
    Replies:
    2
    Views:
    520
  4. Thelma Roslyn Lubkin
    Replies:
    1
    Views:
    352
    Erik Wikström
    Dec 28, 2007
  5. Greg Hauptmann
    Replies:
    0
    Views:
    294
    Greg Hauptmann
    Feb 6, 2009
Loading...

Share This Page