I have a variable k
of type const char *
, and a function in glib with the prototype
void g_hash_table_replace(GHashTable *hash_table,
gpointer key,
gpointer value);
gpointer
is defined simply as
typedef void* gpointer;
I know that in this case it is, in fact, okay to pass in k
as the key in g_hash_table_replace
, however gcc gives me the error
service.c:49:3: warning: passing argument 2 of ‘g_hash_table_replace’ discards ‘const’ qualifier from pointer target type [enabled by default]
/usr/include/glib-2.0/glib/ghash.h:70:13: note: expected ‘gpointer’ but argument is of type ‘const char *’
this is with gcc 4.6.0. With 4.5.0 and earlier, a simple cast to (char *) sufficed to supress this warning, but gcc seems to have gotten 'smarter'. I've tried (char *)(void *)k
, but it still knows that the variable was originally const
. What is the best way to silence this warning without calling strdup(3)
on k
?
I just tried this with gcc 4.6.1.
#include <glib/ghash.h>
#include <stdio.h>
#include <unistd.h>
const char *k="Testing";
int main(int argc, char **argv)
{
int val = 1024;
GHashTable *hash_table=NULL;
g_hash_table_replace(hash_table,(gpointer) (intptr_t)k, &val);
return 0;
}
Without casts, the error is as you describe above. But if I cast the const char*
to intptr_t
first as shown above, the warning is suppressed. Can you confirm that you still experience the error with my code sample?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With