考虑下面的代码片段:
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。为什么?