How to correctly address -Wcast-qual

I have a ktype variable const char *, and a function in glib with a prototype

void g_hash_table_replace(GHashTable *hash_table,
                          gpointer key,
                          gpointer value);

gpointer defined simply as

typedef void* gpointer;

I know that in this case, in fact, you can pass kin as the key in g_hash_table_replace, however gcc gives me an 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, just clicking on (char *) is enough to suppress this warning, but gcc seems to have gotten smarter. I tried (char *)(void *)k, but he still knows that there was originally a variable const. How can I turn off this warning without calling strdup(3)on k?

+5
source share
1 answer

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;
}

, . const char* intptr_t, , . , - ?

+2

All Articles