GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Sep 17, 2014 1:32 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 
Offline
Familiar Face

Joined: Tue Nov 20, 2007 1:55 pm
Posts: 8
Hi,
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);

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.
Thanks,
sumit kumar


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 27, 2007 8:05 pm 
Offline
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
http://www.micahcarrick.com


Top
 Profile  
 
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: Yahoo [Bot] and 2 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group