## Type Conversion Macros Type Conversion Macros — portably storing integers in pointer variables |

## Functions

#define | GINT_TO_POINTER()
#define | GPOINTER_TO_INT()
#define | GUINT_TO_POINTER()
#define | GPOINTER_TO_UINT()
#define | GSIZE_TO_POINTER()
#define | GPOINTER_TO_SIZE()

## Includes

    #include <glib.h>

## Description

Many times GLib, GTK+, and other libraries allow you to pass "user data" to a callback, in the form of a void pointer. From time to time you want to pass an integer instead of a pointer. You could allocate an integer, with something like:

    1
    2
    int *ip = g_new (int, 1);
    *ip = 42;

But this is inconvenient, and it's annoying to have to free the memory at some later time.

Pointers are always at least 32 bits in size (on all platforms GLib intends to support). Thus you can store at least 32-bit integer values in a pointer value. Naively, you might try this, but it's incorrect:

    1
    2
    3
    4
    gpointer p;
    int i;
    p = (void*) 42;
    i = (int) p;

Again, that example was not correct, don't copy it. The problem is that on some systems you need to do this:

    1
    2
    3
    4
    gpointer p;
    int i;
    p = (void*) (long) 42;
    i = (int) (long) p;

The GLib macros `GPOINTER_TO_INT()`, `GINT_TO_POINTER()`, etc. take care to do the right thing on every platform.

Warning: You may not store pointers in integers. This is not portable in any way, shape or form. These macros only allow storing integers in pointers, and only preserve 32 bits of the integer; values outside the range of a 32-bit integer will be mangled.

## Functions

### GINT_TO_POINTER()

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

Stuffs an integer into a pointer type.

Remember, you may not store pointers in integers. This is not portable in any way, shape or form. These macros only allow storing integers in pointers, and only preserve 32 bits of the integer; values outside the range of a 32-bit integer will be mangled.

#### Parameters

i | integer to stuff into a pointer |

### GPOINTER_TO_INT()

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

Extracts an integer from a pointer. The integer must have been stored in the pointer with `GINT_TO_POINTER()`.

Remember, you may not store pointers in integers. This is not portable in any way, shape or form. These macros only allow storing integers in pointers, and only preserve 32 bits of the integer; values outside the range of a 32-bit integer will be mangled.

#### Parameters

p | pointer containing an integer |

### GUINT_TO_POINTER()

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

Stuffs an unsigned integer into a pointer type.

#### Parameters

u | unsigned integer to stuff into the pointer |

### GPOINTER_TO_UINT()

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

Extracts an unsigned integer from a pointer. The integer must have been stored in the pointer with `GUINT_TO_POINTER()`.

#### Parameters

p | pointer to extract an unsigned integer from |

### GSIZE_TO_POINTER()

    #define GSIZE_TO_POINTER(s) ((gpointer) (gsize) (s))

Stuffs a gsize into a pointer type.

#### Parameters

s | gsize to stuff into the pointer |

### GPOINTER_TO_SIZE()

    #define GPOINTER_TO_SIZE(p) ((gsize) (p))

Extracts a gsize from a pointer. The gsize must have been stored in the pointer with `GSIZE_TO_POINTER()`.

#### Parameters

p | pointer to extract a gsize from |