簡體   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