一句话解释:union 与结构体定义方式一样,只是所有变量在一个地址上而已。
例子:
#include <stdio.h>
union _test
{
int a;
int b;
}test,*ptest;
int main(void) {
ptest = &test;
//两个变量在同一个地址
//先存入的数据覆盖整个地址
ptest->a = 1;
//b的值也为1
printf("b= %d",ptest->b);
return 0;
}
运行结果:
b= 1