RE: Example in Docs: GPOINTER_TO_INT
- From: Lion Kimbro <lion lithtech com>
- To: gtk-app-devel-list gnome org
- Subject: RE: Example in Docs: GPOINTER_TO_INT
- Date: Thu, 31 May 2001 14:59:03 -0700
Dear Ruben,
It's primarily a convenience;
Here's the selection out of Pennington
(http://www.andamooka.org/reader.pl?pgid=ggadcha_glib):
--------------------------
There are also many macros unique to glib, such as the portable
gpointer-to-gint and gpointer-to-guint conversions shown in Figure 2.
Most of glib's data structures are designed to store a gpointer. If you want
to store pointers to dynamically allocated objects, this is the right thing.
However, sometimes you want to store a simple list of integers without
having to dynamically allocate them. Though the C standard does not strictly
guarantee it, it is possible to store a gint or guint in a gpointer variable
on the wide range of platforms glib has been ported to; in some cases, an
intermediate cast is required. The macros in Figure 2 abstract the presence
of the cast.
Here's an example:
gint my_int;
gpointer my_pointer;
my_int = 5;
my_pointer = GINT_TO_POINTER(my_int);
printf("We are storing %d\n", GPOINTER_TO_INT(my_pointer));
Be careful, though; these macros allow you to store an integer in a pointer,
but storing a pointer in an integer will not work. To do that portably, you
must store the pointer in a long. (It's undoubtedly a bad idea to do so,
however.)
#include <glib.h>
GINT_TO_POINTER(p);
GPOINTER_TO_INT(p);
GUINT_TO_POINTER(p);
GPOINTER_TO_UINT(p);
Figure 2. Macros for storing integers in
--------------------------
I suppose you could make a whole structure just to store a pointer, but
it's easier to just use the macro.
Be sure that ints fit w/in ptrs.
Take care,
Lion {:)}=
-----Original Message-----
From: Brooklyn Linux Solutions [mailto:ruben mrbrklyn com]
Sent: Wednesday, May 30, 2001 8:29 PM
To: Daniel V .
Cc: Brooklyn Linux Solutions; gtk-app-devel-list gnome org
Subject: Re: Example in Docs: GPOINTER_TO_INT
(gint) to_print
The example puts an int in the pointer "slot" instead of a pointer, by
casting it to the pointer type. It then retrieves the number by casting
the pointer back to an int.
Maybe not the best example, but commonly used nonetheless.
I'm sorry to take this into a digression, why would we want to
cast a pointer, which has a memory address stored in it, to an integer?
Ruben
Brooklyn Linux Solutions
http://www.mrbrklyn.com
http://www.brooklynonline.com
1-718-382-5752
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]