在STM32 MDK实现类似__attribute__((__packed__))效果

__attribute__是GNU  C对标准C语法的扩展,是GNU C的一大特色,可以用于设置函数的属性,变量的属性,类型的属性



在STM32 MDK实现类似效果;
实验数据如下:
代码
void printRecData(void)
{
    typedef struct{
        uint16_t      tpdb_01;                                                     // 存储器中有效的录音文件数量   
        uint8_t       tpdb_02;                                                       // 当前模块音量
        uint16_t      tpdb_03;                                                     // 已录时间
        uint16_t      tpdb_04;                                                    // 已录空间
        uint16_t      tpdb_05;                                                 // 当前播放的编号
        uint32_t      tpdb_06;
        uint8_t       tpdb_07;
    }typedef_TPrecData;
typedef_TPrecData TPrecData
= { .tpdb_01 = 0x0123, // 存储器中有效的录音文件数量 .tpdb_02 = 0x45, // 当前模块音量 .tpdb_03 = 0x67, .tpdb_04 = 0xabcd, // 已录空间 .tpdb_05 = 0xef10, // 当前播放的编号 .tpdb_06 = 0xaabbccdd, .tpdb_07 = 0x76, }; uint8_t i = 0; uint8_t * ptr= (uint8_t *) &TPrecData; for(i = 0;i<sizeof(TPrecData);i++){ printf("%02x ", ptr[i]); } printf("\n"); }

输出结果为:23 01 45 00 67 00 cd ab 10 ef 00 00 dd cc bb aa 76 00 00 00 

 

修改代码如下:

void printRecData(void)
{
    typedef __packed struct {
        uint16_t      tpdb_01;                                                     // 存储器中有效的录音文件数量   
        uint8_t       tpdb_02;                                                       // 当前模块音量
        uint16_t      tpdb_03;                                                     // 已录时间
        uint16_t      tpdb_04;                                                    // 已录空间
        uint16_t      tpdb_05;                                                 // 当前播放的编号
        uint32_t      tpdb_06;
        uint8_t       tpdb_07;
    }typedef_TPrecData;
typedef_TPrecData TPrecData
= { .tpdb_01 = 0x0123, // 存储器中有效的录音文件数量 .tpdb_02 = 0x45, // 当前模块音量 .tpdb_03 = 0x67, .tpdb_04 = 0xabcd, // 已录空间 .tpdb_05 = 0xef10, // 当前播放的编号 .tpdb_06 = 0xaabbccdd, .tpdb_07 = 0x76, }; uint8_t i = 0; uint8_t * ptr= (uint8_t *) &TPrecData; for(i = 0;i<sizeof(TPrecData);i++){ printf("%02x ", ptr[i]); } printf("\n"); }

输出结果为:23 01 45 67 00 cd ab 10 ef dd cc bb aa 76 

 

posted on 2017-03-27 15:54  Taylor-Yeung  阅读(739)  评论(0编辑  收藏  举报

导航