也许一个不兼容的指针可以访问变量
#include <stdio.h>
#include <stdlib.h>
typedef struct father {
int age;
char* name;
} father;
typedef struct student {
int age;
double height;
char sex;
} student;
int main() {
// initiate two stuct variables
student* s = (student*)malloc(sizeof(student));
father* f = (father*)malloc(sizeof(father));
s->age = 22;
s->height = 1.78;
s->sex = 'm';
f->age = 45;
f->name = "Alan";
// typecasting
// f指向学生结构体变量,f会将这个变量看作father结构体变量并访问
f = (student*)s;
/*正常情况下,输出的是一个异常值,但要看具体的内存布局!!
本例中,father和student变量都有一个成员age,而且都是第一个成员,
因此,f访问student变量所在的内存区域,刚好可以成功访问f的age这个变量
*/
printf("f->age = %d\n", f->age);
// 正常情况下,输出的是一个异常值,但要看具体的内存布局!!
printf("f->name = %d\n", f->name);
return 0;
}