How to use GList Double Link List



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

*GList *list = NULL;
GList *listrunner;
gint array[] = { 1, 2, 3, 4, 5, 6 };
gint pos;
gint *value;

/* add data to the list */
for (pos=0;pos < 6; pos++) {
printf("adding to the list::%d,sizeof array:%d\n",
array[pos],sizeof(array));
list = g_list_append(list, (gpointer)&array[pos]);
}

/* run through the list */
listrunner = g_list_first(list);
while (listrunner) {
value = (gint *)listrunner->data;
printf("data:%d\n", *value);
listrunner = g_list_next(listrunner);
}

/* removing datas from the list */
listrunner = g_list_first(list);
list = g_list_remove_link(list, listrunner);
list = g_list_remove(list, &array[4]);
*
It works fine.But when I modified this program to take input from user I
prints only last data I appends to the list:-




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

Please tell me links where I can find more examples of GList.
Thanks,
sumit



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