I want to achieve having a linked list in which every link stores 2 integer values. I wanted to achieve this by putting a nested struct inside the glist link. But I get an error which states that my struct is an incompatible data type. What should I do now? I wrote the following Program:
#include <glib.h>
/* this is the documentation of the glist struct:
struct GList {
gpointer data;
GList* next;
GList* prev;
}
*/
typedef struct client_data {
int int1;
int int2;
};
int main(){
//initialize glist
GList *client_datalist = NULL;
//Data storage which should be temporary because I want to store the data inside the list
struct client_data tempstruct;
tempstruct.int1 = 1;
tempstruct.int2 = 100;
g_list_append (client_datalist, tempstruct);
}
I would appreciate any help with this. I would think there could be a possibility to add 2 integers into the glist link directly but I don't know how I should do that.
client_datalist=g_list_append (client_datalist, tempstruct);. If you do not keep the return value of the initial call ong_list_append, you will just leak the memory used by the newly created list element. Additionally a GList is not a C++ container: it only stores pointers to objects and the caller is responsable for allocating and freeing the objects.typedef actual_type alias_name;. You have forgotten thealias_namepart, and only defined the structure tag and type. Thestruct client_data { ... }is theactual_typepart in your code. That should give build errors.int). You could do e.g.typedef struct foo { /* some members... */ } bar;and usebaras the type:bar my_foo_structure;