Type Conversion Macros

Name

Type Conversion Macros -- a portable method for storing gint & guint values in gpointer variables.

Synopsis


#include <glib.h>


#define     GINT_TO_POINTER                 (i)
#define     GPOINTER_TO_INT                 (p)

#define     GUINT_TO_POINTER                (u)
#define     GPOINTER_TO_UINT                (p)

Description

These macros provide a portable method of storing gint and guint values in gpointer variables.

Many of the GLib data types are based on storing gpointer values, e.g. GHashTable, GList, GSList, GTree, and GNode. By using the type conversion macros described below you can store gint and guint values inside a gpointer. So you can, for example, create a hash table of gint values, or a linked list of guint values.

The type conversion macros are necessary because the size of a gpointer can vary across different platforms. So the type conversion has to be done carefully.

Note that the reverse operation, storing gpointer values in integer variables, is not supported, since an integer is not guaranteed to be large enough to store gpointer values across all platforms.

To convert an integer value, a gint, to a gpointer, use GINT_TO_POINTER. To convert it back to a gint, use GPOINTER_TO_INT.

To convert an unsigned integer, a guint, to a gpointer, use GUINT_TO_POINTER. To convert it back to a guint, use GPOINTER_TO_UINT.

Details

GINT_TO_POINTER()

#define GINT_TO_POINTER(i)	((gpointer)  (i))

Converts a gint to a gpointer.

i :a gint value.
Returns :the value converted to a gpointer.


GPOINTER_TO_INT()

#define GPOINTER_TO_INT(p)	((gint)   (p))

Converts a gpointer to a gint.

p :a gpointer value.
Returns :the value converted to a gint.


GUINT_TO_POINTER()

#define GUINT_TO_POINTER(u)	((gpointer)  (u))

Converts a guint to a gpointer.

u :a guint value.
Returns :the value converted to a gpointer.


GPOINTER_TO_UINT()

#define GPOINTER_TO_UINT(p)	((guint)  (p))

Converts a gpointer to a guint.

p :a gpointer value.
Returns :the value converted to a guint.