送分喽,三行代码,欢迎大家进来溜达联络感情!
#include "stdio.h"
struct student
{
public:
int x;
int y;
};
void main()
{
struct student * stu=new struct student();
stu->x=5;
stu->y=13;
int * x=(int *)stu;
printf("%d",*x);
}
这样输出的是5,如果 int * x=(int *)stu+1;则输出的是13,我相信这个结果大家都知道,我也知道!
接下来一个我就不是很清楚了,求大家帮忙,我也知道问题出在结构体内存布局的分析上。
#include "stdio.h"
struct student
{
public:
//int x;
//short x;
char x;
double y;
};
void main()
{
struct student * stu=new struct student();
stu->x=5;
stu->y=13;
int * x=(int *)stu+2;
printf("%f",*x);
}
这个输出时0.0000,真的没想到。我调试过程已经发现了,x=0x00382280,&stu->y=0x00382200
但是也看到了*x=0,stu->y=13.00000000000000,请问这里地址相同,为什么结果不等?先说谢谢!