[9479] in The GTK GIMP ToolKit mailing list archive

home help back first fref pref prev next nref lref last post

[gtk-list] Re: GList problem

daemon@ATHENA.MIT.EDU (Ronan BOURLIER)
Thu Oct 22 03:27:30 1998

Date: Thu, 22 Oct 1998 09:29:04 +0000
From: Ronan BOURLIER <ronan@radioss.com>
To: gtk-list@redhat.com
Resent-From: gtk-list@redhat.com
Reply-To: gtk-list@redhat.com

Wesley Lemke wrote:
> 
> I'm having problems creating a list of strings.  I'm new to gtk, so
> this problem should be a simple one for you gurus :)
> 
> Here is my code:
> 
> for(i = 1; i <= WEEKS; i++)
> {
>         sprintf(string, "Week %d", i);
>         list = g_list_append(list, string);
>         printf("%s\n", string);
> }
> 
> g_list_foreach(list, (GFunc) PrintList, NULL);
> 
> The printf statement prints what I'd expect...."Week 1, Week 2, Week 3..etc"
> However when I got thought the list with the foreach function, and print
> out each value it shows that all the values are "Week 17"  (17 is what
> WEEKS is defined to).  So the last value is getting inserted into the list
> WEEKS times.....what am I doing wrong?
> 
Hi Buck
you need to add a malloc to create a new string
for each item of the list.
something like this
for(i = 1; i <= WEEKS; i++)
{
	string=g_malloc(YOUR_MAX_LENGTH*sizeof(gchar));
        sprintf(string, "Week %d", i);
        list = g_list_append(list, string);
        printf("%s\n", string);
}

Ronan

-- 
To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null


home help back first fref pref prev next nref lref last post