天天看点

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

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

测试代码:

#include <stdio.h>

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;
}