Explicit ignore warning from -Wcast-qual: cast discards ‘__attribute__((const))’ qualifier from pointer target type
Solution 1
In GCC 4.2 and later, you can suppress the warning for a function by using #pragma. The disadvantage is you have to suppress the warning across the whole function; you cannot just use it only for some lines of code.
#pragma GCC diagnostic push // require GCC 4.6
#pragma GCC diagnostic ignored "-Wcast-qual"
void foo(){
const char* ptr = buf;
/* ... */
char* q = (char*)ptr;
}
#pragma GCC diagnostic pop // require GCC 4.6
The advantage is your whole project can use the same warning/errors checking options. And you do know exactly what the code does, and just make GCC to ignore some explicit checking for a piece of code.
Because the limitation of this pragma, you have to extract essential code from current function to new one, and make the new function alone with this #pragma.
Solution 2
#include <stdint.h>
const char * ptr = buf;
....
char * p = (char *)(uintptr_t)ptr;
Or, without stdint.h:
char * p = (char *)(unsigned long)ptr;
Comments
-
ext almost 2 years
static char buf[8]; void foo(){ const char* ptr = buf; /* ... */ char* q = (char*)ptr; }
The above snippet will generate
"warning: cast discards ‘__attribute__((const))’ qualifier from pointer target type [-Wcast-qual]"
. I like-Wcast-qual
since it can help me from accidentally writing to memory I shouldn't write to.But now I want to cast away const for only a single occurrence (not for the entire file or project). The memory it is pointing to is writable (just like
buf
above). I'd rather not drop const fromptr
since it is used elsewhere and keeping to pointers (one const and one non-const) seems like a worse idea. -
David Ranieri almost 11 yearsEnabled by default warnings: The GNU compiler collection (GCC) 4.6.3 might generate enabled by default warnings that cannot be suppressed.
-Wcast-qual
is one of them:warning: initialization discards ‘const’ qualifier from pointer target type **[enabled by default]**
-
R.. GitHub STOP HELPING ICE almost 10 years@AlterMann: That's a completely different warning that occurs when you make an implicit conversion which discards the
const
qualifier (which is invalid C; C does not have such an implicit conversion). It's not the warning OP is seeing. -
malat about 9 yearswarning is still there using gcc 4.7.2
-
André Fratelli over 7 yearsI would discourage this approach. Don't work around the warnings. If you don't want them, use suppressions, which is what they are meant for. This will be explicit about your intentions and make your code much more readable.
-
Bruce K almost 7 yearsSuppressions are not generally portable. And if you share your code, someone will compile with something that hates your suppression method. If you want to be explicit: #define UN_CONSTIFY(_t, _v) ((_t)(uintptr_t)(_v))
-
David Ranieri over 2 yearsShouldn't it be
char * p = (char *)(void *)(uintptr_t)(void *)ptr;
instead ofchar * p = (char *)(uintptr_t)ptr;
? AFAIK a conversion to void * is required before converting a pointer to uintptr_t