Re: How to use GList Double Link List



On Dec 25, 2007 9:50 PM, sumit kumar <kchaudhary sumit gmail com> wrote:
Hi All,
I am trying to use GLib double link list in my application.But I have a
problem. I run the GList sample program from Gtk+-2.0 FAQ:-

*snip*

*gint pos;
g_print("Enter Number1\n");
scanf("%d",&pos);
list = g_list_append(list, (gpointer)&pos);

g_print("Enter Number2\n");
scanf("%d",&pos);
list = g_list_append(list, (gpointer)&pos);

g_print("Enter Number3\n");
scanf("%d",&pos);
list = g_list_append(list, (gpointer)&pos);

g_print("Enter Number4\n");
scanf("%d",&pos);
list = g_list_append(list, (gpointer)&pos);
*
It prints only last element I appened 4 times... 44, 44 ,44 ,44 if I
appended 44 as last element.
Whats wrong with this code??


This is not a GTK question, so wouldn't this be the wrong list?

Anyway, your code will read in a number (using scanf) into a variable
called "pos", which is overwritten by the next call to scanf. Since
your list only stores pointers to "pos", you have four pointers to the
same variable, which stores the last number entered by the user.
Change your list to directly store the numbers themselves, or allocate
new memory for each integer.



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