<refentry id="glib-Type-Conversion-Macros">
<refmeta>
<refentrytitle>Type Conversion Macros</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GLIB Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Type Conversion Macros</refname><refpurpose>
portably storing integers in pointer variables.</refpurpose>
</refnamediv>

<refsynopsisdiv><title>Synopsis</title>
<synopsis>

#include &lt;glib.h&gt;


#define     <link linkend="GINT-TO-POINTER-CAPS">GINT_TO_POINTER</link>                 (i)
#define     <link linkend="GPOINTER-TO-INT-CAPS">GPOINTER_TO_INT</link>                 (p)

#define     <link linkend="GUINT-TO-POINTER-CAPS">GUINT_TO_POINTER</link>                (u)
#define     <link linkend="GPOINTER-TO-UINT-CAPS">GPOINTER_TO_UINT</link>                (p)
#define     <link linkend="GSIZE-TO-POINTER-CAPS">GSIZE_TO_POINTER</link>                (s)
#define     <link linkend="GPOINTER-TO-SIZE-CAPS">GPOINTER_TO_SIZE</link>                (p)
</synopsis>
</refsynopsisdiv>





<refsect1>
<title>Description</title>
<para>
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:
<informalexample><programlisting>
 int *ip = g_new (int, 1);
 *ip = 42;
</programlisting></informalexample>
But this is inconvenient, and it's annoying to have to free the 
memory at some later time.
</para>
<para>
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:
<informalexample><programlisting>
 gpointer p;
 int i;
 p = (void*) 42;
 i = (int) p;
</programlisting></informalexample>
Again, that example was <emphasis>not</emphasis> correct, don't copy it. 
The problem is that on some systems you need to do this:
<informalexample><programlisting>
 gpointer p;
 int i;
 p = (void*) (long) 42;
 i = (int) (long) p;
</programlisting></informalexample>
So <link linkend="GPOINTER-TO-INT-CAPS">GPOINTER_TO_INT</link>(), <link linkend="GINT-TO-POINTER-CAPS">GINT_TO_POINTER</link>(), etc. do the right thing
on the current platform.
</para>
<para>
<warning>
<para>
YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS NOT PORTABLE IN ANY
WAY SHAPE OR FORM. These macros <emphasis>ONLY</emphasis> 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.
</para>
</warning>
</para>
</refsect1>

<refsect1>
<title>Details</title>
<refsect2>
<title><anchor id="GINT-TO-POINTER-CAPS">GINT_TO_POINTER()</title>
<programlisting>#define GINT_TO_POINTER(i)	((gpointer)  (i))
</programlisting>
<para>
Stuffs an integer into a pointer type.
</para>
<para>
Remember, YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS NOT PORTABLE
IN ANY WAY SHAPE OR FORM. These macros <emphasis>ONLY</emphasis> 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.
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>i</parameter>&nbsp;:</entry>
<entry>integer to stuff into a pointer.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="GPOINTER-TO-INT-CAPS">GPOINTER_TO_INT()</title>
<programlisting>#define GPOINTER_TO_INT(p)	((gint)   (p))
</programlisting>
<para>
Extracts an integer from a pointer. The integer must have
been stored in the pointer with <link linkend="GINT-TO-POINTER-CAPS">GINT_TO_POINTER</link>().
</para>
<para>
Remember, YOU MAY NOT STORE POINTERS IN INTEGERS. THIS IS NOT PORTABLE
IN ANY WAY SHAPE OR FORM. These macros <emphasis>ONLY</emphasis> 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.
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>p</parameter>&nbsp;:</entry>
<entry>pointer containing an integer.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="GUINT-TO-POINTER-CAPS">GUINT_TO_POINTER()</title>
<programlisting>#define GUINT_TO_POINTER(u)	((gpointer)  (u))
</programlisting>
<para>
Stuffs an unsigned integer into a pointer type.
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>u</parameter>&nbsp;:</entry>
<entry>unsigned integer to stuff into the pointer.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="GPOINTER-TO-UINT-CAPS">GPOINTER_TO_UINT()</title>
<programlisting>#define GPOINTER_TO_UINT(p)	((guint)  (p))
</programlisting>
<para>
Extracts an unsigned integer from a pointer. The integer must have
been stored in the pointer with <link linkend="GUINT-TO-POINTER-CAPS">GUINT_TO_POINTER</link>().
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>p</parameter>&nbsp;:</entry>
<entry>pointer to extract an unsigned integer from.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="GSIZE-TO-POINTER-CAPS">GSIZE_TO_POINTER()</title>
<programlisting>#define GSIZE_TO_POINTER(s)	((gpointer) (gsize) (s))
</programlisting>
<para>
Stuffs a <link linkend="gsize">gsize</link> into a pointer type.
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>s</parameter>&nbsp;:</entry>
<entry><link linkend="gsize">gsize</link> to stuff into the pointer.


</entry></row>
</tbody></tgroup></informaltable></refsect2>
<refsect2>
<title><anchor id="GPOINTER-TO-SIZE-CAPS">GPOINTER_TO_SIZE()</title>
<programlisting>#define GPOINTER_TO_SIZE(p)	((gsize) (p))
</programlisting>
<para>
Extracts a <link linkend="gsize">gsize</link> from a pointer. The <link linkend="gsize">gsize</link> must have
been stored in the pointer with <link linkend="GSIZE-TO-POINTER-CAPS">GSIZE_TO_POINTER</link>().
</para><informaltable pgwide="1" frame="none" role="params">
<tgroup cols="2">
<colspec colwidth="2*">
<colspec colwidth="8*">
<tbody>
<row><entry align="right"><parameter>p</parameter>&nbsp;:</entry>
<entry>pointer to extract a <link linkend="gsize">gsize</link> from.


</entry></row>
</tbody></tgroup></informaltable></refsect2>

</refsect1>




</refentry>
