Summary: | slave.cpp:475: warning: cast from type 'char**' to type 'const char**' casts away constness | ||
---|---|---|---|
Product: | Gentoo Linux | Reporter: | David van Laatum <david> |
Component: | [OLD] Core system | Assignee: | Gentoo Linux bug wranglers <bug-wranglers> |
Status: | RESOLVED INVALID | ||
Severity: | normal | CC: | jcallen |
Priority: | High | ||
Version: | unspecified | ||
Hardware: | All | ||
OS: | Linux | ||
Whiteboard: | |||
Package list: | Runtime testing required: | --- |
Description
David van Laatum
2009-08-20 10:20:13 UTC
-Wcast-qual Warn whenever a pointer is cast so as to remove a type qualifier from the target type. For example, warn if a "const char *" is cast to an ordinary "char *". (In reply to comment #1) > -Wcast-qual > > Warn whenever a pointer is cast so as to remove a type qualifier > from the target type. For example, warn if a "const char *" is > cast to an ordinary "char *". > thats my point casting so as to remove the const is wrong but adding it isn't.... which is what its complaining about in this case #include <stdlib.h> int main ( int argc, char **argv ) { char *buffer[1] = { NULL }; const char **test = (const char**)buffer; return 0; } gcc -o test -Wcast-qual test.cpp test.cpp: In function 'int main(int, char**)': test.cpp:5: warning: cast from type 'char**' to type 'const char**' casts away constness no warning if compiled with gcc 3.4.6-r2 The warning is correct. A cast from 'char**' to 'const char**' is not as harmless as you might at first think. If it were legal to convert without a cast, it would enable the following: const char a = 'a'; // this is a constant it should never change char *x; const char **y = &x; // in reality this is illegal *y = &a; // now x points to a *x = 'b'; // now we have modified a As this would allow modifying 'const' variables, it is disallowed. You can, however, convert 'char**' to 'const char* const*', which doesn't have the same issues. |