warning: initialization discards qualifiers from pointer target type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andre

    #1

    warning: initialization discards qualifiers from pointer target type

    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_tab le[] = {
    { "deflate", DEFLATE, DEFLATE_WINDOW_ SIZE }, <-- HERE
    { "gzip", GZIP,
    24 }, <-- HERE
    { "", -1,
    0 } <-- HERE
    };
  • Michael Mair

    #2
    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_tab le[] = {
    { "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_tab le[] = {
    { "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_ta ble' 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.

    Comment

    • Eric Sosman

      #3
      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_tab le[] = {
      { "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@sun .com

      Comment

      • Micah Cowan

        #4
        Re: warning: initialization discards qualifiers from pointer target type

        Eric Sosman <Eric.Sosman@su n.comwrites:
        Andre wrote:
        >Hi All,
        >>
        >When I compile the following piece of code with gcc, I get 3 "warning:
        >initializati on 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_tab le[] = {
        > { "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 "essentiall y 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...

        Comment

        • Andrey Tarasevich

          #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

          Comment

          Working...