c语言类型转换时不报错
c++会报错
因为c++强语言
C++提供4种方式:
static_cast<类型>(数据)用于数值类型之间以及void*和别的*类型之间
reinterpret_cast<类型>(数据)用于数值类型与地址类型之间或地址相互之间
const_cast<T*>(T常量的地址)去掉对地址所指向的目标的const限制。
dynamic_cast
#include<iostream>
using namespace std;
int main()
{
/浮点型转换整型
int a = static_cast<int>(123.456);
int b = 100;
void* p = &b;
//void*转换
int* q = static_cast<int*>(p);
char* r =reinterpret_cast<char*>(q);
cout << "*r=" << r <<endl;
///const常量强制转换
const int c = b;
cout << "c=" << c <<endl;
*const_cast<int*>(&c) = 200;
cout << "c=" << c <<endl;
return 0;
}