[gnutls-devel] Casting from int to (void *)

Emile van Bergen emile at vanbergen.name
Fri Feb 8 12:34:42 CET 2013


Hi,

Am I correct in that you effectively need the reverse of an intptr_t (C99,
an integer with sufficient size to hold a pointer), namely a pointer having
a width that is guaranteed to be sufficient to hold an ordinary integer?

In that case the assumption that sizeof(int) <= sizeof(void *) is probably
safe on all platforms that GnuTLS runs on. For sizeof(long) this may not be
the case.

Aside, to avoid direct casting between pointers and integers, an option
may be to write void *p = (char *)0 + i, exploiting the fact that
sizeof(char) == 1 by ISO definition. The only cast is the null pointer;
otherwise there is only the implicit cast from char * to void *. You go the
other way around by writing i = (char *)p - (char *)0;

Note however that for this construct too, the compiler is not likely to
warn you on platforms where sizeof(int) > sizeof(void *).

Kind regards,


Emile.



More information about the Gnutls-devel mailing list