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

发布于 2024-11-04 07:34:14 字数 1794 浏览 1 评论 0原文

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")

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

尝试使用 (gboolean *) 等收到了来自 gtk 的警告,内容如下:

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 *’
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"));

The 3 errors below correspond to the 3 if statements above.

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

If you will allow me to extract the offending parts:

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

Why are these typecasts causing these errors? How can I fix them?

Trying to use (gboolean *) etc recieved warnings from gtk along the lines of:

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 *’

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶花眉 2024-11-11 07:34:14

xmlGetProp 函数 返回一个字符串(如 xmlChar *):

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

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

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

The xmlGetProp function returns a string (as xmlChar *):

Search and get the value of an attribute associated to a node This does the entity substitution. This function looks in DTD attribute declaration for #FIXED or default declaration values unless DTD use has been turned off.
[...]
Returns: the attribute value or NULL if not found. It's up to the caller to free the memory with xmlFree().

The caller is responsible for parsing that string into whatever form (floating point, integer, boolean, ...) is needed. Also note that the caller is responsible for freeing the returned xmlChar * string too.

To fix your problem, you need to convert the string to what you need in the usual manner. And don't forget to xmlFree the returned strings.

羁客 2024-11-11 07:34:14

我像这样修复了它:

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

仍然不知道为什么会起作用,但我可以猜测。

I fixed it like so:

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

Still don't know why that worked, but I can guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文