编译器类型转换警告和错误

时间:2011-04-29 18:44:31

标签: c compiler-construction casting gtk glib

if(xmlStrEquals(cur->name, (const xmlChar *) "check")) // Find out which type it is
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (gtk_builder_get_object (builder, xmlGetProp(cur,"name"))),(gboolean) xmlGetProp(cur,"value"));

else if(xmlStrEquals(cur->name, (const xmlChar *) "spin"))
    gtk_adjustment_set_value(GTK_ADJUSTMENT (gtk_builder_get_object (builder, xmlGetProp(cur,"name"))),(gdouble) xmlGetProp(cur,"value"));

else if(xmlStrEquals(cur->name, (const xmlChar *) "combo"))
    gtk_combo_box_set_active(GTK_COMBO_BOX (gtk_builder_get_object (builder, xmlGetProp(cur,"name"))),(gint) xmlGetProp(cur,"value"));

下面的3个错误对应于上面的3个if语句。

main.c:125: warning: cast from pointer to integer of different size
main.c:130: error: pointer value used where a floating point value was expected
main.c:139: warning: cast from pointer to integer of different size

如果您允许我提取有问题的部分:

(gboolean) xmlGetProp(cur,"value")
(gdouble) xmlGetProp(cur,"value")
(gint) xmlGetProp(cur,"value")

为什么这些类型转换会导致这些错误?我该如何修理它们?

尝试使用来自gtk的(gboolean *)等收到的警告:

warning: passing argument 2 of ‘gtk_toggle_button_set_active’ makes integer from pointer without a cast
/usr/include/gtk-2.0/gtk/gtktogglebutton.h:82: note: expected ‘gboolean’ but argument is of type ‘gboolean *’
error: incompatible type for argument 2 of ‘gtk_adjustment_set_value’
/usr/include/gtk-2.0/gtk/gtkadjustment.h:93: note: expected ‘gdouble’ but argument is of type ‘gdouble *’
warning: passing argument 2 of ‘gtk_combo_box_set_active’ makes integer from pointer without a cast
/usr/include/gtk-2.0/gtk/gtkcombobox.h:99: note: expected ‘gint’ but argument is of type ‘gint *’

2 个答案:

答案 0 :(得分:1)

xmlGetProp function返回一个字符串(xmlChar *):

  

搜索并获取与节点关联的属性的值这将执行实体替换。除非已关闭DTD,否则此函数会在#FIXED或默认声明值的DTD属性声明中查找   [...]
  返回:属性值,如果未找到,则返回NULL。调用者可以使用xmlFree()来释放内存。

调用者负责将该字符串解析为任何形式(浮点,整数,布尔值等)。另请注意,调用者也负责释放返回的xmlChar *字符串。

要解决您的问题,您需要以通常的方式将字符串转换为您需要的字符串。并且不要忘记xmlFree返回的字符串。

答案 1 :(得分:0)

我这样修好了:

(gboolean) *xmlGetProp(cur,"value")
(gdouble) *xmlGetProp(cur,"value")
(gint) *xmlGetProp(cur,"value")

仍然不知道为什么会有效,但我可以猜到。