Use glib

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MN

    Use glib

    Hi all,
    I'm learning how to use some Glib-functions for simple linked list. I
    wrote a small program that prepends 2 data (let's say to integers "1"
    and "2") in list, display them, count total number of elements and
    returns index of each element.
    How to avoid these warnings?
    warning: passing argument 1 of ‘g_list_nth’ from incompatible pointer
    type
    warning: format ‘%d’ expects type ‘int’, but argument 2 has type
    ‘struct GList *’
    warning: passing argument 1 of ‘g_list_index’ from incompatible
    pointer type
    warning: passing argument 2 of ‘g_list_index’ makes pointer from
    integer without a cast

    My code is:

    #include <stdio.h>
    #include <stdlib.h>
    #include <glib.h>

    int main ()
    {

    GSList *list = NULL;

    // Add data
    list = g_slist_prepend (list, GINT_TO_POINTER (2));
    list = g_slist_prepend (list, GINT_TO_POINTER (1));

    // Print data
    while (list)
    {
    printf("data is %d\n", GPOINTER_TO_INT (list -data));
    list = list -next;
    }
    printf("\n");

    // Get the element at poistion number
    printf("First element is \"%d\"\n", g_list_nth(list , 0));
    printf("Second element is \"%d\"\n", g_list_nth(list , 1));
    printf("\n");

    // Count number of elements
    printf("Total number of elements is %d\n", g_slist_length (list));
    printf("\n");

    // Get the position of the element
    printf("This should be 0: '%d'\n", g_list_index(li st, 1));
    printf("This should be 1: '%d'\n", g_list_index(li st, 2));
    return 0;
    }

  • MisterE

    #2
    Re: Use glib

    This is off topic here.

    Anyway you seem to be using GSList * instead of GList * or vias versa.
    If you are using GSList * you should be using g_slist_index not g_list_index
    etc. etc. etc.
    Look up GList and GSList and make sure you are using the right one.


    "MN" <mazouz.nezhate @gmail.comwrote in message
    news:c1f735c8-7ede-43e7-a868-cfa0b8fcf292@p5 9g2000hsd.googl egroups.com...
    Hi all,
    I'm learning how to use some Glib-functions for simple linked list. I
    wrote a small program that prepends 2 data (let's say to integers "1"
    and "2") in list, display them, count total number of elements and
    returns index of each element.
    How to avoid these warnings?


    Comment

    • MN

      #3
      Re: Use glib

      I must use g_slist_nth and g_slist_index functions.

      Comment

      • CBFalconer

        #4
        Re: Use glib

        MN wrote:
        >
        I must use g_slist_nth and g_slist_index functions.
        If you want to post a followup via groups.google.c om, ensure you
        quote enough for the article to make sense. Google is only an
        interface to Usenet; it's not Usenet itself. Don't assume your
        readers can, or ever will, see any previous articles. Your message
        above is worthless.

        More details at: <http://cfaj.freeshell. org/google/>

        --
        [mail]: Chuck F (cbfalconer at maineline dot net)
        [page]: <http://cbfalconer.home .att.net>
        Try the download section.

        Comment

        • Antoninus Twink

          #5
          Re: Use glib

          On 20 Oct 2008 at 11:18, MN wrote:
          How to avoid these warnings?
          These warnings are the least of your worries.

          Your code is fundamentally wrong in many ways. Mainly you seem to have a
          misconception of how data is stored in the linked list: the data field
          for a node in the list is a void* which you need to make point to the
          data you actually want to store.
          // Add data
          list = g_slist_prepend (list, GINT_TO_POINTER (2));
          GINT_TO_POINTER doesn't do what you think it does.
          // Print data
          while (list)
          {
          printf("data is %d\n", GPOINTER_TO_INT (list -data));
          list = list -next;
          }
          At this point, list is NULL. You've forgotten the first element of your
          linked list.
          // Get the element at poistion number
          printf("First element is \"%d\"\n", g_list_nth(list , 0));
          Someone else has pointed out that you need g_slist_nth_dat a here. The
          return value is a pointer, which you need to dereference to get your int
          back.
          printf("This should be 0: '%d'\n", g_list_index(li st, 1));
          Similarly, this is wrong: you need to pass a pointer, not an integer.

          Here is a fixed-up version of your code, which may point you in the
          right direction...


          #include <stdio.h>
          #include <stdlib.h>
          #include <glib.h>

          int main(void)
          {
          GSList *list = NULL, *iter;
          int x=2, y=1;

          // Add data
          list = g_slist_prepend (list, &x);
          list = g_slist_prepend (list, &y);

          // Print data
          iter=list;
          while (iter) {
          printf("data is %d\n", *(int *)(iter->data));
          iter = iter->next;
          }
          putchar('\n');

          // Get the element at poistion number
          printf("First element is \"%d\"\n", * (int *)(g_slist_nth_ data(list, 0)));
          printf("Second element is \"%d\"\n", * (int *)(g_slist_nth_ data(list, 1)));
          putchar('\n');

          // Count number of elements
          printf("Total number of elements is %d\n", g_slist_length( list));
          putchar('\n');

          // Get the position of the element
          printf("This should be 0: '%d'\n", g_slist_index(l ist, &y));
          printf("This should be 1: '%d'\n", g_slist_index(l ist, &x));
          return 0;
          }

          Comment

          Working...