Casting to pointer from integer

This is a discussion on Casting to pointer from integer within the C Programming forums, part of the General Programming Boards category; I'm porting my code over to 64-bit and get the "casting to pointer from integer of different size" warning from: ...

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Casting to pointer from integer

    I'm porting my code over to 64-bit and get the "casting to pointer from integer of different size" warning from:
    Code:
    (void *)(int)foo
    I can understand it warning about a loss of information (casting down), but casting up isn't dangerous and the compiler should no more complain than had I done (long long)(int). Anyways I suppose that's compiler philosophy. What I need to know is how do I shut it up without resorting to #ifdef's?
    This does not work:
    Code:
    (void *) (sizeof(void *) == sizeof(long long) ? (long long)foo : foo)
    only other alternative I can think of is -Wno-int-to-pointer-cast
    Last edited by @nthony; 06-29-2010 at 03:11 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    What if you cast to long long and then cast to void*? But that looks like a hack, heheh.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by laserlight View Post
    What if you cast to long long and then cast to void*? But that looks like a hack, heheh.
    yep, I should have mentioned I don't want to anger my 32-bit build either

    But I remembered reading something from opengroup.org on this, and it came back to me: intptr_t is the answer! It's an integer type with the same size as a pointer:
    Code:
    (void*)(intptr_t)foo

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Why do you need to cast a pointer to an integer in the first place, though?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  2. Casting pointer to unsigned short
    By cks2k2 in forum C Programming
    Replies: 6
    Last Post: 03-25-2009, 06:33 AM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21