繁体   English   中英

我不了解此C代码的行为(具有2个位域结构,一个单词和一个字节数组的联合)

[英]I don't understand the behavior of this C code (union with 2 bit-field structs, a word and a byte array)

我在C中有以下代码:

typedef union _REG_CiFIFOCON {
    struct {
        uint32_t RxNotEmptyIE : 1;
        uint32_t RxHalfFullIE : 1;
        uint32_t RxFullIE : 1;
        uint32_t RxOverFlowIE : 1;
        uint32_t unimplemented1 : 1;
        uint32_t RxTimeStampEnable : 1;
        uint32_t unimplemented2 : 1;
        uint32_t TxEnable : 1;
        uint32_t UINC : 1;
        uint32_t unimplemented3 : 1;
        uint32_t FRESET : 1;
        uint32_t unimplemented4 : 13;
        uint32_t FifoSize : 5;
        uint32_t PayLoadSize : 3;
    } rxBF;

    struct {
        uint32_t TxNotFullIE : 1;
        uint32_t TxHalfFullIE : 1;
        uint32_t TxEmptyIE : 1;
        uint32_t unimplemented1 : 1;
        uint32_t TxAttemptIE : 1;
        uint32_t unimplemented2 : 1;
        uint32_t RTREnable : 1;
        uint32_t TxEnable : 1;
        uint32_t UINC : 1;
        uint32_t TxRequest : 1;
        uint32_t FRESET : 1;
        uint32_t unimplemented3 : 5;
        uint32_t TxPriority : 5;
        uint32_t TxAttempts : 2;
        uint32_t unimplemented4 : 1;
        uint32_t FifoSize : 5;
        uint32_t PayLoadSize : 3;
    } txBF;
    uint32_t word;
    uint8_t byte[4];
} REG_CiFIFOCON;

两个结构均为32位,因此它是字变量和字节数组(因为它由4个字节组成。4x8 = 32位)。

我的问题是:我不了解此联合的行为。 我知道如何访问每个结构以及字和数组中的位,但是它们之间有何关系? 我知道如果只有1个结构和单词,将单词设置为某个值会相应地修改位字段(反之亦然),但是我不知道在这种情况下会发生什么。

谢谢你,祝你有美好的一天!

同一联盟中有4种类型。 他们都使用相同的内存。

更改其中一个无关紧要-会影响其他。

您的类型的大小为32个字节-在您的情况下,它也是其中每个类型的大小。 否则-这将是内部最大类型的大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM