Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 静态\u将整数地址转换为指针_C++_Casting - Fatal编程技术网

C++ 静态\u将整数地址转换为指针

C++ 静态\u将整数地址转换为指针,c++,casting,C++,Casting,为什么你需要一个C风格的演员 int* ptr = static_cast<int*>(0xff); // error: invalid static_cast from type 'int' // to type 'int*' int* ptr = (int*) 0xff; // ok. int*ptr=static_cast(0xff);//错误:类型“int”的静态\u转换无效 //要键入“int*”

为什么你需要一个C风格的演员

int* ptr = static_cast<int*>(0xff); // error: invalid static_cast from type 'int' 
                                    // to type 'int*'
int* ptr = (int*) 0xff; // ok.
int*ptr=static_cast(0xff);//错误:类型“int”的静态\u转换无效
//要键入“int*”
int*ptr=(int*)0xff;//好啊

在将整数转换为指针时,您需要使用C样式转换,或者直接使用
重新解释转换
它的意思,因为标准对不相关的类型是这样规定的

标准要求这些演员在那里,因为

  • 你在那里做一些危险的事
  • 你正在做一些很少有用的事情
  • 您正在做一些高度依赖于实现的事情
  • 大多数时候,这只是一个编程错误


  • 静态\u强制转换
    只能在两个相关的类型之间进行强制转换。整数与指针无关,反之亦然,因此您需要使用
    重新解释\u cast
    ,它告诉编译器重新解释整数的位,就像它们是指针一样(反之亦然):

    int*ptr=reinterpret\u cast(0xff);
    
    有关更多详细信息,请阅读以下内容:


    您还可以使用C风格演员最终将使用的
    重新解释演员阵容。不过还是不是个好主意。
    
    int* ptr = reinterpret_cast<int*>(0xff);