简体   繁体   English

在C ++中将double *转换为int *

[英]Casting double* to int* in C++

The title's pretty self explanatory. 标题非常自我解释。 I want to cast a type double * into a type int * . 我想将类型double *转换为类型int * I realize that I can use the C-type cast (int *) to do what I want, but is there a way to do this cast using C++ type casting ie static_cast etc? 我意识到我可以使用C-type强制转换(int *)来做我想要的,但有没有办法使用C ++类型转换,即static_cast等进行此转换?

You can perform this cast using a reinterpret_cast : 您可以使用reinterpret_cast执行此reinterpret_cast

int* veryUnsafePointer = reinterpret_cast<int*>(myDoublePointer);

Be aware that this does not give you back an integer representation of the double being pointed at; 请注意,这并不会返回指向的double的整数表示形式; instead, the integer's value will be dependent on the binary representation of the double and the endianness of the system. 相反,整数的值将取决于double的二进制表示和系统的endianness。

Hope this helps! 希望这可以帮助!

You need to use reinterpret_cast<int *>(ptr) . 您需要使用reinterpret_cast<int *>(ptr)

I hope you really know what you're doing though. 我希望你真的知道你在做什么。 There's very little reason to do such a cast, especially when it's likely a double and an int are different sizes. 没有理由做这样的演员,特别是当它可能是一个double和一个int是不同的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM