warning: initialization discards qualifiers from pointer target type

A

Andre

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
};
 
M

Michael Mair

Andre said:
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

Eric Sosman

Andre said:
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*'.
 
M

Micah Cowan

Eric Sosman said:
Andre said:
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.
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.
 
A

Andrey Tarasevich

Andre said:
...
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top