小编Bob*_*ers的帖子

如何正确地解决-Wcast-qual

我有一个k类型的变量const char *,以及glib中的原型函数

void g_hash_table_replace(GHashTable *hash_table,
                          gpointer key,
                          gpointer value);
Run Code Online (Sandbox Code Playgroud)

gpointer 被简单地定义为

typedef void* gpointer;
Run Code Online (Sandbox Code Playgroud)

我知道在这种情况下,实际上可以k将密钥作为密钥传入g_hash_table_replace,但是gcc给了我错误

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 *’
Run Code Online (Sandbox Code Playgroud)

这是用gcc 4.6.0.对于4.5.0及更早版本,一个简单的转换为(char*)足以抑制此警告,但gcc似乎已经变得"更聪明"了.我试过了(char *)(void *)k,但它仍然知道变量是最初的const.什么是沉默不调用此警告的最佳方式strdup(3)k

c gcc gcc-warning

5
推荐指数
1
解决办法
2063
查看次数

标签 统计

c ×1

gcc ×1

gcc-warning ×1