0
考虑下面的代码片断:C++双PTR到长的ptr转换
double * p = new double[16];
int value = 1;
void * q = p;
*((double *)q) = value;
int x = ((long *)p)[0];
cout << "Value of x for double * to long * = " << x << endl;
*((int *)q) = value ;
x = ((long *)p)[0];
cout << "Value of x for int * to long * = " << x << endl;
这里的输出分别为0和1。任何人都可以向我解释为什么?
此外,如果我直接访问指针值...即。 p [0],这两种情况下的值都正确显示为1。为什么?
所有这些类型转换都被视为代码异味。不要在真实的代码中使用。 – iammilind
这基本上是'reinterpret_cast'。该标准不能保证你甚至可以得到什么结果(即从有效的结果)。 –