g_list problem



Hello, 

I have a problem with allocation of first element in a GList. This GList 
should de declarated in the main function, but contents are computed into 
another function (included into another file). I've written a small program 
to illustrate my purpose :

the secondary file is :
#include <gtk/gtk.h>

int function(GList *list)
{
        list = g_list_append(list, "First");
        list = g_list_append(list, "Second");
        list = g_list_append(list, "Third");

        return 1;
}

and in the main file I put :
#include <gtk/gtk.h>

int main()
{
        GList *list = NULL;
        
        function(list);
        g_print("word is %s \n", (char *)list->data);
        
        return 0;
}

This is, I guess, the traditional way to use GLists. But in this case, it's 
doesn't work. This is not surprising because a null pointer is passed to 
function() and a not null-pointer should be returned because of 
g_list_append().

To workaround this problem I allocate un empty element before of calling 
function() and I remove this element after function(). Here is the code of 
main file (secondary file doesn't change) :

#include <gtk/gtk.h>

int main()
{
        GList *list = g_list_alloc();
        
        function(list);
        list = g_list_remove(list, g_list_nth_data(list, 0));
        g_print("word is %s \n", (char *)list->data);
        
        return 0;
}

This is working fine, but this solution isn't very elegant. Is there is 
another way to solve this problem ?

Thanks for any help.


-- 
______________________________________________________________________________
Jean-Max Redonnet                        mailto:redonnetNOSP Mlgmt ups-tlse fr
please replace NOSP M with @ for reply
remplacez NOSP M par @ pour répondre



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