我直接贴代码了。
#include <stdio.h>
struct A
{
int a;
int b;
};
int main()
{
struct A test;
double *p = (double *)&(test.a);
*p = 5;
printf("variable a: %d\n", &test.a);
printf("variable b: %d\n", &test.b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在centos7中运行这段代码,编译器是.gcc4.8.5并且我的电脑使用little end来存储。
正如你所看到的,变量的内存b将被覆盖,我期望的a是 is0x0000 0005和bis 0x0000 0000。
但答案是:
variable a: 0
variable b: 1075052544
Run Code Online (Sandbox Code Playgroud)
为什么变量a是0x 0000 0000并且b是0x4014 0000?