473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Use glib

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;
}

Oct 20 '08 #1
4 4594
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" <ma************@gmail.comwrote in message
news:c1**********************************@p59g2000 hsd.googlegroups.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?
Oct 20 '08 #2
MN
I must use g_slist_nth and g_slist_index functions.
Oct 20 '08 #3
MN wrote:
>
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/>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 20 '08 #4
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_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;
}

Oct 21 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel C Bastos | last post by:
intro: i have a simple c program that uses gtk (1.2) which in turn uses glib. using the flag -Wall i get no warnings. using -Wall -ansi no warnings. using -Wall -ansi -pedantic I get these...
5
by: Erwan Loisant | last post by:
Hello. I can't properly add elements to a GArray. Basically I have a GArray of GList, so here is what I do to append an element in a given GList of my GArray: 1. GList* currentBucket =...
2
by: Arjan | last post by:
Hi, I want to open a serial port (COM1) in my Windows program (C), and I would like to use the Glib IOchannels to do this. Does anybody know how to do this? Examples maybe? Thanks in advance,...
0
by: TPJ | last post by:
Is there a place to talk about problems with the GLib library? I was looking for something on www.gtk.org, but all I found were some mailing lists about GTK+.
1
by: Loic Mahe | last post by:
Hello, I would like to compile GLib as a static library on Windows XP with MinGW if possible. I downloaded GLib and needed librairies, but only the .dll files are included in the zip file. ...
1
by: lumpybanana247 | last post by:
~~~"glib.h: No such file or directory. "~~~ I am using DevC++ to compile a program and it says "glib.h: No such file or directory. " I do not have any parameters set up, but it does appear...
10
by: akappa | last post by:
Hi, I want to serialize a glib data type (an hash), but I don't want to access directly to the fields of the various structs, since it's an opaque data type: there is one "more-or-less standard...
1
by: ramasubramanian.rahul | last post by:
hi people.. dont know if this the right forum for this doubt ... so sorry if i am mis-posting... i was looking at the way glib 2.10.3 does export optimization using a list of "to be exposed"...
37
by: Michael Palmer | last post by:
As anyone knows, the state of Python GUI programming is a little fractured at this time, with many toolkits, wrappers and meta-wrappers dead and alive, with or without documentation. I've come...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.