Re: Glib -> GList



On Tue, 2002-12-31 at 23:21, MET @ Uber wrote:
> I'm trying without any luck to find documentation on using GList in a 
> situation with more than one node.  I know that it's basic structure contains 
> a 'next' pointer and a void pointer called 'data'.  While researching it I 
> read something about using the void data pointer to be able to point to 
> anything such as another structure, but I haven't found any examples.  This 
> email proves that I am not a very experienced programmer, so using a void 
> pointer to me is brand new.  Any suggestions or links to suggestions on using 
> GList with my own data (as it appears to be great) would be very appreciated.
> 
> Thanks in advance,
> 
> ~ Metnetsky

A void pointer just means an untyped pointer, or a pointer to anything.

Typically you use the "data" member of a GList/GSList when you want to
retrieve the data from a list item:

Example:

GList *list = NULL;
gchar *str = "some string";

list = g_list_append (list, str);

g_message ("The contents of the first node in list is \"%s\".", (gchar
*) (list->data));

This creates a new GList item with the data set to str ("some string"),
and appends it the end of the (empty) GList, "list".

The g_message output would be:

some-app (pid) Message: The contents of the first node in list is "some
string".

Since it's a void pointer, you can store anything you want as the data,
like GtkWidgets, your own structs, ints (using GINT_TO_POINTER()), etc.

Basically, rather than creating a new list type for every data type you
want to store, doing it with a gpointer means you can re-use the same
code for anything (though it also means that it's a good idea to see if
the contents of "data" are valid before doing anything, and it also
means deep freeing the list is not as simple as g_list_free()).

-- 
Peace,

    Jim Cape
    http://ignore-your.tv

    "It is literally true that, like Christianity, Socialism
     has conquered the world by defeating itself."
        -- Alexander Berkman, ABC of Anarchism



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