"Jason" <> wrote in message
news:bm027n$uu0$...
> 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
message:
> warning: assignment discards qualifiers from pointer target type
>
> Does anyone know what this warning means?
It means you're walking on eggs.
> Why do I get it?
You have a good compiler.
>The program
> compiles and appears to work, but I'd like to understand the warning.
The moment you try to modify what the assigned-to pointer
points to, you could possibly induce undefined behavior.
>
> Here is basically what the code looks like:
>
> char *str;
> str = Inet_ntop(...); //returns const char *
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 */
> Any help is appreciated. Thanks.
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