structures and GList



Hello,

I have a problem with the following code to retrieve the informations
stored from a structure in a list.
Theres something wrong, because I append the elements and then,
executing the code, I can only retrieve the last element and not the
others.


Here's the code:



struct.h:

/* --- BOF --- */
typedef struct tTest
{
  gboolean plop;
  GList *list;

} tTest;

typedef struct structure_t
{
  gint16 x, y;
} structure_t;

extern tTest Test;
/* --- EOF --- */



test.c:

/* --- BOF --- *.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include "struct.h"

tTest Test;

int main(void)
{
  structure_t *mylist;

  Test.list = NULL;

  mylist = g_malloc(sizeof(mylist));

  mylist->x = 3;
  mylist->y = 4;
  Test.list = g_list_append(Test.list, mylist);
  mylist->x = 8;
  mylist->y = 1;
  Test.list = g_list_append(Test.list, mylist);
  mylist->x = 5;
  mylist->y = 7;
  Test.list = g_list_append(Test.list, mylist);

  for ( ; Test.list ; Test.list = g_list_next(Test.list)) {

    structure_t *element;

    element = (structure_t *)Test.list->data;

    printf("->x=%d\n", element->x);
  }
  
  return 0;
}
/* --- EOF --- */


and the out is :(:
toady wellington:~/test$ ./test
->x=5
->x=5
->x=5
toady wellington:~/test$


Thanks for your help,



-- 
Sébastien Tricaud <stricaud mwsp net>




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