yanshanbao的笔记 https://bbs.21ic.com/?779501 [收藏] [复制] [RSS]

日志

联合union的用法

已有 311 次阅读2016-12-27 13:51 |个人分类:C语言、程序学习|系统分类:嵌入式系统| union, 联合


例子
#include "io430.h"
union iForm
{
  unsigned int  i;
  unsigned char c[2];
};//占用空间 = 最大的变量占用的空间
struct iForm1
{
  unsigned int i;
  unsigned char c[2];
};//占用空间 = 依次占用空间之和
__no_init union iForm DetFlag             @0x0200;    //测试union  占用的内存空间
__no_init struct iForm1 DetFlag_1          @0x0202;    //测试struct 占用的内存空间
/*******************************************************************************
* Function: main()
*
* Description: 采用硬件仿真的方式区分union和struct
*
* Calls:       --
* Called by:   --
* Globals:     DetFlag DetFlag_1
* Input:       void
* Output:      Null
* Return:      void
* Others:      Null
*******************************************************************************/
void main(void)
{
   
  WDTCTL = WDTPW + WDTHOLD;
  DetFlag.i = 0x55aa;
  DetFlag.c[0] = 0x11;
  DetFlag.c[1] = 0x22;
 
  DetFlag_1.i = 0x55aa;
  DetFlag_1.c[0] = 0x11;
  DetFlag_1.c[1] = 0x22;
 
  for(;;)
  {
//    DetFlag.i = 0x01;
  }
}
运行结果:


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)