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. Advertisements

  2. Andre

    Michael Mair Guest

    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
     
    Michael Mair, Mar 3, 2008
    #2
    1. Advertisements

  3. Andre

    Eric Sosman Guest

    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

    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 Cowan, Mar 4, 2008
    #4
  5. 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.
     
    Andrey Tarasevich, Mar 4, 2008
    #5
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.