【问题标题】:Anonymous structures, unions in C99 Keil MDK5C99 Keil MDK5 中的匿名结构、联合
【发布时间】:2017-05-27 13:43:58
【问题描述】:

我正在尝试使大量数据更易于管理。

#pragma anon_unions

typedef union
{
        uint8_t Contents[PACKET_SIZE];

        struct
        {
            uint8_t Command;
            uint8_t X[24];
            uint8_t Y[24];
            uint8_t Z[16];
            uint8_t something;
            .
            .
            .


        };

        struct
        {
            uint8_t Command; // have to rename to dummy_Command to avoid compiler error
            uint8_t A;
            uint8_t B[6];
            uint8_t C[48];
            .
            .
            .

        };
} PacketToFromFile;

有没有办法在没有任何编译器错误的情况下仍然使用相同的名称,例如“命令”?

【问题讨论】:

    标签: c structure c99 unions keil


    【解决方案1】:

    也许……

    #pragma anon_unions
    
    typedef union {
            uint8_t Contents[PACKET_SIZE];
    
            struct {
                uint8_t Command;
                union {
                    struct {
                        uint8_t X[24];
                        uint8_t Y[24];
                        uint8_t Z[16];
                        uint8_t something;
                          :
                          :
                    };
                    struct {
                        uint8_t A;
                        uint8_t B[6];
                        uint8_t C[48];
                          :
                          :
                   };
            }
        }
    } PacketToFromFile;
    

    我没有 Keil 编译器,但我认为这应该可以。

    【讨论】:

    • 它实际上比问题中的要复杂得多。但是你的回答给了我解决它的想法。有用。谢谢@SGeorgiades
    猜你喜欢
    • 2011-03-14
    • 2014-10-21
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 2012-07-13
    • 1970-01-01
    相关资源
    最近更新 更多