This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: pointer <-> integer conversion warnings (bogus -Wall warnings)


On 13-Mar-2002, Tom Lord <lord@emf.net> wrote:
> > From: Tim Hollebeek <tim@hollebeek.com>
> > Why not:
> > 
> > 	 x = (int)(intptr_t)p;
> 
> Because it is certainly not portable and is not as nearly as I can
> tell, part of ISO/IEC 9899:1990 or IEEE 1003.1-1990.

So autoconf your own `intptr_t', if the system doesn't support it.

Or, use the following:

	/*
	** When casting from `int' to a pointer type, you should
	** first cast to `intptr_cast_type'.  This is a type
	** that is (a) the same size as a pointer, on most platforms,
	** to avoid compiler warnings about casts from pointer to int of
	** different size; and (b) guaranteed to be at least as big as
	** `int'.
	*/
	#if __STDC_VERSION__ >= 199901
	  #include <inttypes.h>
	  #if INTPTR_MAX >= INT_MAX
	    typedef intptr_t intptr_cast_type;
	  #else /* no intptr_t, or intptr_t smaller than `int' */
	    typedef intmax_t intptr_cast_type;
	  #endif
	#else
	  #include <stddef.h>
	  #include <limits.h>
	  #if PTRDIFF_MAX >= INT_MAX
	    typedef ptrdiff_t intptr_cast_type;
	  #else
	    typedef int intptr_cast_type;
	  #endif
	#endif

	...
 	x = (int)(intptr_cast_type)p;

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]