Use glib

M

MN

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(list, 1));
printf("This should be 1: '%d'\n", g_list_index(list, 2));
return 0;
}
 
M

MisterE

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.


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?
 
C

CBFalconer

MN said:
I must use g_slist_nth and g_slist_index functions.

If you want to post a followup via groups.google.com, 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/>
 
A

Antoninus Twink

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_data 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(list, 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(list, &y));
printf("This should be 1: '%d'\n", g_slist_index(list, &x));
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top