代码如下:
/*****************************
*功能:联合体测试
*时间:2019年11月28日
****************************/
#include <stdio.h>
#include <math.h>
typedef union test{
int a;
char b;
}u_test,*test_p;
int main(int argc, const char *argv[])
{
u_test s;//定义联合体
test_p p=&s;//定义指针
p->a=100;
p->b='m';
printf("a:%d b:%c\n",p->a,p->b);//读的结果:a:109 b:m, 109是m的ascii码值
//说明a和b共用内存,最后赋值覆盖前面的赋值
return 0;
}
结束。