Re: Compile warning (newbie)



no need to declare *str, like this:

void edit_changed (GtkWidget *widget, gpointer *data)
{
    gchar *strcopy;

    strcopy = g_strdup(gtk_entry_get_text (GTK_ENTRY (widget)));

    g_print ("Entry changed to %s\n", strcopy);

    g_free(strcopy);
}



On Apr 17, 2007, at 8:09 AM, William D. Tallman wrote:

On Mon, Apr 16, 2007 at 05:01:29PM -0600, Michael L Torrie wrote:
On Tue, 2007-04-17 at 08:22 +1000, Andrew Cowie wrote:
Return type of gtk_entry_get_text() is (const gchar*), not just
(gchar*). You discarded the const qualifier when assigning the result to
str.

Just declare str with const.

The reason why this is important is because gtk_entry_get_text is
returning you just a pointer to a buffer inside the widget. Therefore you should never modify it. If you do, you run the risk of crashing the program. That's why the return type is const and the compiler gives you
a warning.

If you want to do something to the string you need to copy it with
g_strdup, remembering to free your copy when you are done.

Michael

Then it should be like this?

void edit_changed (GtkWidget *widget, gpointer *data)
{
    const gchar *str;
    gchar *strcopy;

    str = gtk_entry_get_text (GTK_ENTRY (widget));

    strcopy = g_strdup(str);

    g_print ("Entry changed to %s\n", strcopy);

    free(strcopy);
}

I'm no whiz with C itself and have never used strdup.  IIUC, the
template is as above, replacing the g_print with whatever is to be done
with the copied string.  Is that correct?  If not, what don't I
understand here?

Thanks,

Bill Tallman

_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]