__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
转载于:https://www.cnblogs.com/jlicon/p/6627146.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)