Re: Glib -> GList
- From: Norm Reitzel <NormReitzel swbell net>
- To: gtk-list gnome org
- Subject: Re: Glib -> GList
- Date: Thu, 02 Jan 2003 15:54:29 -0600
Metnetsky:
It's easier than it seems. Suppose you have defined a structure to hold some data about a patient. This structure might contain stuff about his diseases, his white count, his last treatment date, etc.
struct patient { int count; // white count
time_t last_treated; // date last treated
char *complaint; // whassamatta
};
You would allocate one of these structures and fill it in with data ( though I would use a class, and fill it in in the constructor ).
struct patient *sick_guy;
sick_guy = new struct patient;
sick_guy->count = his_white_count(); // fill in the data, etc.
THen you could insert this structure into a Glist as so:
GList *list_of_patients = new GList;
list_of_patients->data = (void *) sick_guy;
Later on, if you wanted to refer to this item, you would use a reverse cast to assign the void pointer from data to a typed pointer, like so:
sick_guy = ( struct patient * ) ( list_of_patients->data );
The whole purpose of a void pointer is that it is untyped, and hence can hold whatever kind of data you want it to point to.
You should read the sections of K&R on pointers carefully, as C manages pointers quite well, but it LEAVES THE MANAGMENT OF THE DATA entirely to YOU, the programmer. Part of the reason for C++ and its classes, and Java, is that many people have a lot of difficulty managing data objects via pointers.
Don't know if that helps or not.
-- Norm Reitzel
> I was wondering if you, or anyone else could explain how to use the data void
> pointer in GList to point to another structure. Any kind of structure would
> do, the simpler the better I'd imagine. Thanks in advance, and I hope this
> isn't something too complicated to email. Oh, if you know of a site that has
> this in some tutorial/example/documentation form that would be perfect too.
>
> ~ Metnetsky
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]