结构体之间的强制类型转换

2014-11-23 20:00:42 · 作者: · 浏览: 13

\


#include 

struct A
{
	int num;
};

struct B
{
	int num;
	char type;
	int age;
};

int main(void)
{
	struct A a;
	a.num = 1;
	
	char* temp1 = (char*)(&(a.num));
	temp1 = temp1 + 4;
	*temp1 = 'a';
	
	int* temp2 = (int*)(&(a.num));
	temp2 = temp2 + 2;
	*temp2 = 100;
	
	struct B* b = (struct B*)(&a);
	
	printf("b->
num=%d b->type=%c b->age=%d\n", b->num, b->type, b->age); return 0; }