Casting 32-bit number as pointer gives size warning RRS feed

  • Question

  • Consider the following code:


    void main ()


    unsigned long number32 = 0x00;

    unsigned long value32;

    // cast a 32-bit number as a pointer, get value at address.  Get warning about size.

    value32 = *(unsigned long *) number32;

    // first cast the number as 64 bits, then as a pointer, then get value at that address.  No warning.

    value32 = *(unsigned long *) (long long) number32;



    The objective is to use a 32-bit number as an address and retrieve the value at that address.  The first assignment above results in this warning:


    "warning C4312: 'type cast' : conversion from 'unsigned long' to 'unsigned long *' of greater size"


    The second assignment first casts the 32-bit number as a 64-bit number, then casts it as a pointer.  No warning from this line.  This would seem to imply that a pointer is wider than 32 bits.  I believe this is incorrect when running 32-bit Windows.


    In this very simple example, there are obviously several ways to avoid this warning, including declaring number32 as a pointer and avoiding the type casting.  But I am really interested in why the compiler generates a warning in this exact case.



    Wednesday, April 4, 2007 2:12 PM


All replies