LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2017, 06:00 AM   #1
yakkmeister
Member
 
Registered: Jan 2005
Location: QLD Australia
Distribution: Debian 10 'Buster'
Posts: 78

Rep: Reputation: 16
C - Adding n nodes to a slist with struct - glib


Hello,

I'm trying to wrap my head around using glib for its list and sorting routines, in the hope that I'd be doing myself a favour in using robust code ...

I discovered that I have 2 big problems here, one of which there may be no solution to.

What I need to do is create n nodes, insert them into the list, calculate a number then sort them based on the number.

I recognised that I would need a struct for the node data as GSList only has a single pointer and single variable.
GSList

Problem is, each struct would need to be named, so I expect that I would need to have anticipated every name ... which means I don't really think I'm approaching the problem correctly ... I suspect there may be no direct remedy for this.

Second, I get an error at compile-time - it complains that it expected GCompareFunc but got something like GInt.
I do apologise for not having the exact error - I'll paste it when I get to work tomorrow (my home machine doesn't like to locate any libs at all so I can't reproduce it... :/ )
Suffice to say, I don't get what it wants in terms of being a GCompareFunc.



Details:

I have been using this IBM tutorial on data management with Glib.

Eventually, I want to be able to read a series of numbers from a file (probably a .csv) then add that number of new items to my list.
The random number simply pretends to be the number from a file.

The following code is, clearly, broken... but maybe it'll help explain what I'm trying to achieve.
I've attempted writing my own lists, I was using a doubly linked list of Struct with next* and prev* pointers in there but I couldn't get it to work - I know, shame on me...

Code:
//lists.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <glib.h>

typedef struct {
	int id;
	int iq;
} Person;

gint my_comparator(gconstpointer item1, gconstpointer item2) {
	return item1 - item2;
}

int main(int argc, char** argv) {
	GSList* list = NULL;
	
	int i;

	srand(time(NULL)); //random number takes the place of file input for now

	for(i = (rand() % 100); i > 0; i--){
		Person* !problem! = g_new(Person, 1); 		// allocate memory for one Person struct
		!problem!->id = i;				// !problem! = here's the problem
		!problem!->iq = (rand() % 200);
		list = g_slist_prepend(list, !problem!); 	//prepend for speed
	}
	list = g_slist_sort(list, (GCompareFunc)my_comparator);	//something really wrong here

	return 0;
}

Last edited by yakkmeister; 03-20-2017 at 06:46 AM. Reason: mucked up the code!
 
Old 03-20-2017, 07:45 AM   #2
yakkmeister
Member
 
Registered: Jan 2005
Location: QLD Australia
Distribution: Debian 10 'Buster'
Posts: 78

Original Poster
Rep: Reputation: 16
Problem solved.

A friend of mine (in Germany, no less) happened to come online and help me out. I wasn't as far off as I thought I was ...
Here's the working code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <glib.h>

typedef struct {
    int id;
    int iq;
} Person;

gint my_comparator(gconstpointer item1, gconstpointer item2)
{
    return ((Person*)item1)->iq - ((Person*)item2)->iq;
}

int main(int argc, char** argv)
{
    GSList* list = NULL;

    int i;

    srand(time(NULL)); //random number takes the place of file input for now

    for(i = (rand() % 100); i > 0; i--){
        Person* problem = g_new(Person, 1);         // allocate memory for one Person struct
        problem->id = i;
        problem->iq = (rand() % 200);
        list = g_slist_prepend(list, problem);  //prepend for speed
    }
    list = g_slist_sort(list, (GCompareFunc)my_comparator); //something really wrong here
    for(GSList *p = list; p; p = p->next)
        printf("%d: %d\n", ((Person*)p->data)->id, ((Person*)p->data)->iq);

    return 0;
}
 
  


Reply

Tags
c/c++, n00b



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting up Kickstart server RHEL 6.5 for compute nodes, GPU nodes and PHI nodes sho1sho1 Red Hat 3 06-23-2015 04:20 PM
error in adding malicius nodes should be solved mary1001 Linux - Software 3 01-23-2015 10:21 AM
adding disks to computing nodes in a diskless cluster mahmoodn Linux - Server 3 12-31-2012 04:52 PM
[SOLVED] Adding new fields to struct page mmmmec Linux - Kernel 1 02-20-2012 05:58 AM
adding to vector<string> in a struct niteshadw Programming 3 02-03-2005 07:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration