简体   繁体   中英

Compiler typecasting warnings and errors

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM