Re: g_list problem



if you want to modify the value of an "int"
in a function call; you have to pass the addresss
of your int. This goes for a ptr-to-GList aswell.

(I think standard C supports 8 levels
of abstraction i.e. "int function(GList ********list);")


solution A:
        GList *list = NULL;
        list = function(list);
                
GList *function(GList *list)
{
        GList *ret = list;
        ret = g_list_append(ret, "data");
        /* ... */
        return ret;
}


solution B:

        GList *list = NULL;
        function(&list);

int function(GList **list)
{
        *list = g_list_append(tmp, "data");
        /* ... */
        return 1;
}

Cheers,
        -Tristan


Jean-Max Redonnet wrote:

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
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



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