[9474] 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 (Dave Cole)
Wed Oct 21 20:56:34 1998

To: gtk-list@redhat.com
From: Dave Cole <dave@dccs.com.au>
Date: 22 Oct 1998 10:56:12 +1000
In-Reply-To: Wesley Lemke's message of "Wed, 21 Oct 1998 19:41:34 -0500 (CDT)"
Resent-From: gtk-list@redhat.com
Reply-To: gtk-list@redhat.com


> 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?

When you add an entry a list, it does not make a copy of it.  The
effect of your code is to make a list with WEEKS entries, all of which
point to the same string.  The string contains the value that was
formatted by the last iteration of the loop.

- Dave

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS dpu s-:+ a C++$ ULS++$ P+++$>++++ L++>+++$ E+>++ W N++ !o K w++$ O !M-
!V(-) PS+ PE- Y+ PGP !t-- 5++ X R tv b+ DI+++ D G e++ h--- r+++ y++++
------END GEEK CODE BLOCK------

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


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