But this is not right, not as approach ( the target variable is of type char* and not const gchar *) and not from the compiler view as well:
error: initialization discards âconstâ qualifier from pointer target type [-Wdiscarded-qualifiers]
I noticed also that some of them just cast it away ( gchar* ) "GTK_TREE_MODEL_ROW".
How should one treat this case? its usage is in this case together with gtk_tree_view_enable_model_drag_source and gtk_tree_view_enable_model_drag_dest.
The GtkTargetEntry structure is an old part of the API, and the meaning of const in C has always been a lot less strict than const in C++. The structure also caters to both static storage allocation and heap allocation, which means it needs to own the data, so that it can free it later on.
In general, if youâre using static strings, you can safely cast away their constness. GTK wonât modify the contents of the GtkTargetEntry structure, anyway.
The alternative is to always allocate GtkTargetEntry structures with gtk_target_entry_new(), and free them when not needed any more with gtk_target_entry_free().
I also had to change G_N_ELEMENTS ( row_targets ) because of:
error: division âsizeof (GtkTargetEntry * {aka struct _GtkTargetEntry *}) / sizeof (GtkTargetEntry {aka struct _GtkTargetEntry})â does not compute the number of array elements [-Werror=sizeof-pointer-div]
to: sizeof ( GtkTargetEntry ) / sizeof ( *row_targets ),
and I hope that is fine.
âHopeâ is a bit too thin a thing to rely on, when dealing with C.
All functions that ask for a GtkTargetEntry in GTK are really asking for an array of GtkTargetEntry; if you allocate a pointer, youâre really allocating an array of 1 element.
If you want to allocate more than one target entry in order to pass it to the GTK API, you will need to do the allocation and freeing yourself:
// Allocate the targets array
GtkTargetEntry *targets = g_new0 (GtkTargetEntry, n_targets)
// Initialize each target
targets[0].target = g_strdup ("foo");
targets[0].flags = ...
targets[0].info = ...
targets[1].target = g_strdup ("bar");
targets[1].flags = ...
targets[1].info = ...
...
// Set the targets
gtk_tree_view_enable_model_drag_dest (treeview, targets, n_targets, action_flags);
// Free the copied "target" string
for (guint i = 0; i < n_targets; i++)
g_free (targets[i].target);
// Free the target array
g_free (targets);
[quote=âebassi, post:4, topic:4586â]
âHopeâ is a bit too thin a thing to rely on, when dealing with C.
[/quote].
You are definitely right on that one Thank you.