Re: Compile warning (newbie)
- From: "William D. Tallman" <wtallman olypen com>
- To: gtk-list gnome org
- Subject: Re: Compile warning (newbie)
- Date: Mon, 16 Apr 2007 23:09:56 -0700
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
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]