Thread: Casting to pointer from integer

  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 02:11 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What if you cast to long long and then cast to void*? But that looks like a hack, heheh.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Location
    Inside my computer
    Posts
    24,654
    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, 03:45 AM
  2. Casting pointer to unsigned short
    By cks2k2 in forum C Programming
    Replies: 6
    Last Post: 03-25-2009, 05: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, 03:36 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM