GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Aug 25, 2016 6:12 am

All times are UTC

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: how to use GLib Double Link List
PostPosted: Mon Dec 24, 2007 9:26 am 
Familiar Face

Joined: Tue Nov 20, 2007 1:55 pm
Posts: 8
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");
list = g_list_append(list, (gpointer)&pos);

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

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

g_print("Enter Number4\n");
list = g_list_append(list, (gpointer)&pos);

Its will print only last element I appened 4 times.
Whats wrong with this code??

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

 Post subject:
PostPosted: Thu Dec 27, 2007 8:05 pm 
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
This is more of a C question really. You might want to read up a bit more on how pointers work. you're simply writing over the same pointer over and over... so each item you add points to the same data.

Micah Carrick - Forum Administrator

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Google Adsense [Bot], Yahoo [Bot] and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group