level 9
zhangjb90s
楼主
最近看书的时侯,发现结构体转换用的很多,有时候还特意的对某个结构体进行填充。之前对这个不是很了解,于是写了些测试的代码。
一.
#include <stdio.h>
#include <conio.h>
typedef struct test1
{
int
num1;
int
num2;
}test1;
typedef struct test2
{
int num3;
int
num4;
int
num5;
}test2;
int main()
{
test1 a;
test1
*p;
test2
b,*q;
p=&a;
q=&b;
a.num1=1;
a.num2=2;
b.num5=5;
q=(test2
*)&a;
printf("%d %d
%d\n",q->num3,q->num4,q->num5);
getch();
return
0;
}
结果:
1 2
1638280
二.
#include <stdio.h>
#include <conio.h>
typedef struct test1
{
int
num1;
int
num2;
}test1;
typedef struct test2
{