| re: warning: assignment discards qualifiers from pointer target type
"Jason" <jake1138@NO.SPAM.yahoo.com> wrote in message
news:bm027n$uu0$1@terabinaries.xmission.com...[color=blue]
> I have a function (Inet_ntop) that returns const char * and if I try to
> assign that return value to a char * variable, I get the gcc error[/color]
message:[color=blue]
> warning: assignment discards qualifiers from pointer target type
>
> Does anyone know what this warning means?[/color]
It means you're walking on eggs. :-)
[color=blue]
> Why do I get it?[/color]
You have a good compiler.
[color=blue]
>The program
> compiles and appears to work, but I'd like to understand the warning.[/color]
The moment you try to modify what the assigned-to pointer
points to, you could possibly induce undefined behavior.
[color=blue]
>
> Here is basically what the code looks like:
>
> char *str;
> str = Inet_ntop(...); //returns const char *[/color]
It's warning you that the "protection" of the pointer's
target provided by the 'const' qualifier is lost when
the 'const' is thrown away by assigning it to a "plain"
type 'char*' pointer.
It's warning you that you're boating in deep water,
and you've thrown your life preserver overboard.
const char array[]="whatever";
array[0] = 'X'; /* compiler must tell you, "Can't do that!" */
const char *cp = array;
cp[0] = 'X'; /* compiler must tell you, "Can't do that!" */
char *p = array; /* valid, but many compilers will warn you:
"Danger, Will Robinson!" */
/* because... */
p[0]; /* is valid, although possibly/probably not what you
really wanted, and has possiblity of undefined behavior */
[color=blue]
> Any help is appreciated. Thanks.[/color]
Write:
const char *str;
str = Inet_ntop(...); //returns const char *
The function returns type 'const char*' for a reason.
It's telling you "You can look, but don't touch the
target of the returned pointer." If you ignore this
warning, you're on your own.
HTH,
-Mike |