How do I cast a pointer to an int

后端 未结 5 2134
一向
一向 2021-02-13 21:54

I\'m trying to store the value of an address in a non pointer int variable, when I try to convert it I get the compile error \"invalid conversion from \'int*\' to \'int\'\" this

5条回答
  •  醉梦人生
    2021-02-13 22:59

    int may not be large enough to store a pointer.

    You should be using intptr_t. This is an integer type that is explicitly large enough to hold any pointer.

        intptr_t thatvalue = 1;
    
        // stuff
    
        thatvalue = reinterpret_cast(ip);
                    // Convert it as a bit pattern.
                    // It is valid and converting it back to a pointer is also OK
                    // But if you modify it all bets are off (you need to be very careful).
    

提交回复
热议问题