Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   warning: initialization discards qualifiers from pointer target type (http://www.velocityreviews.com/forums/t595813-warning-initialization-discards-qualifiers-from-pointer-target-type.html)

Andre 03-03-2008 09:21 PM

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_table[] = {
{ "deflate", DEFLATE, DEFLATE_WINDOW_SIZE }, <-- HERE
{ "gzip", GZIP,
24 }, <-- HERE
{ "", -1,
0 } <-- HERE
};

Michael Mair 03-03-2008 09:35 PM

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.

Eric Sosman 03-03-2008 09:47 PM

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@sun.com

Micah Cowan 03-04-2008 12:44 AM

Re: warning: initialization discards qualifiers from pointer target type
 
Eric Sosman <Eric.Sosman@sun.com> 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/

Andrey Tarasevich 03-04-2008 04:58 AM

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


All times are GMT. The time now is 04:19 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.