本文主要是介绍C语言,struct 结构体、union共用体的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//状态字节,根据数据定义几个标志,标志位依据联合体内部结构体进行变量定义
//目的,节省内存空间,省去特定字节
struct STATDATA
{union{unsigned char stat;struct {unsigned stat0:1;unsigned stat1:1;unsigned stat2:1;unsigned stat3:1;unsigned stat4:1;unsigned stat5:1;unsigned stat6:1;unsigned stat7:1;};};
};
struct STATDATA STATDATA1;
struct STATDATA STATDATA2;int main(void)
{STATDATA1.stat = 0x55;STATDATA2.stat = 0xAA;printf("STATDATA1.stat0 = %d\n",STATDATA1.stat0);printf("STATDATA1.stat1 = %d\n",STATDATA1.stat1);printf("STATDATA1.stat2 = %d\n",STATDATA1.stat2);printf("STATDATA1.stat3 = %d\n",STATDATA1.stat3);printf("STATDATA1.stat4 = %d\n",STATDATA1.stat4);printf("STATDATA1.stat5 = %d\n",STATDATA1.stat5);printf("STATDATA1.stat6 = %d\n",STATDATA1.stat6);printf("STATDATA1.stat7 = %d\n",STATDATA1.stat7);printf("STATDATA2.stat0 = %d\n",STATDATA2.stat0);printf("STATDATA2.stat1 = %d\n",STATDATA2.stat1);printf("STATDATA2.stat2 = %d\n",STATDATA2.stat2);printf("STATDATA2.stat3 = %d\n",STATDATA2.stat3);printf("STATDATA2.stat4 = %d\n",STATDATA2.stat4);printf("STATDATA2.stat5 = %d\n",STATDATA2.stat5);printf("STATDATA2.stat6 = %d\n",STATDATA2.stat6);printf("STATDATA2.stat7 = %d\n",STATDATA2.stat7);return 0;
}
// 输出结果:
// STATDATA1.stat0 = 1
// STATDATA1.stat1 = 0
// STATDATA1.stat2 = 1
// STATDATA1.stat3 = 0
// STATDATA1.stat4 = 1
// STATDATA1.stat5 = 0
// STATDATA1.stat6 = 1
// STATDATA1.stat7 = 0
// STATDATA2.stat0 = 0
// STATDATA2.stat1 = 1
// STATDATA2.stat2 = 0
// STATDATA2.stat3 = 1
// STATDATA2.stat4 = 0
// STATDATA2.stat5 = 1
// STATDATA2.stat6 = 0
// STATDATA2.stat7 = 1
这篇关于C语言,struct 结构体、union共用体的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!