[C语言] 这样引用联合体的成员为什么不行?

[复制链接]
1864|2
 楼主| milan111 发表于 2015-2-7 11:19 | 显示全部楼层 |阅读模式
FRAM, RAM, ni, ui, IO
typedef struct                        //¶¨Òå·¢ËÍÖ¡½á¹¹Ìå
{
    uint8_t  FrameHead_1;
        uint8_t  FrameHead_2;
        uint8_t  FrameID;
        uint8_t  FrameCount;
        int16_t  AngleInt1;
        int16_t  AngleInt2;
        uint8_t  StatusWord;
        int16_t  Rsv;
        uint8_t  TxCheckSum;
} StructTxFrame;

typedef union               //¶¨Òå·¢ËÍÖ¡¹²ÓÃÌå
{
    StructTxFrame TxFrame;
        uint8_t TxBuffer[16];
} UnionTxFrame;
UnionTxFrame UniTxFrame = { 0} ;

UniTxFrame.TxFrame.AngleInt1 = 1;

编译提示最后一行错误: error:  #77-D: this declaration has no storage class or type specifier
                                  error:  #147: declaration is incompatible with "UnionTxFrame UniTxFrame" (decl                                            ared at line 31)
liang7143 发表于 2015-2-7 13:11 | 显示全部楼层
没有不行,赋值本来就不能在函数体外面


typedef struct                        //¶¨Òå·¢ËÍÖ¡½á¹¹Ìå
{
    uint8_t  FrameHead_1;
        uint8_t  FrameHead_2;
        uint8_t  FrameID;
        uint8_t  FrameCount;
        int16_t  AngleInt1;
        int16_t  AngleInt2;
        uint8_t  StatusWord;
        int16_t  Rsv;
        uint8_t  TxCheckSum;
} StructTxFrame;

typedef union               //¶¨Òå·¢ËÍÖ¡¹²ÓÃÌå
{
    StructTxFrame TxFrame;
        uint8_t TxBuffer[16];
} UnionTxFrame;
UnionTxFrame UniTxFrame = { 0} ;

void test(void)
{
         UniTxFrame.TxFrame.AngleInt1 = 1;
}
xyz549040622 发表于 2015-2-8 20:53 | 显示全部楼层
参考楼上的,注意结构体成员的赋值问题。要想在函数外赋值,必须整体赋值。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

42

主题

62

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部