typedef __packed struct 中的__packed意义

本文介绍了C语言中__packed关键字用于定义紧凑结构体,避免填充字节,以节省存储空间。通过typedef为数据类型创建新名称。示例代码展示了如何使用这些特性来减少结构体大小。

typedef __packed struct 是C语言中用于定义紧凑结构体的语法。__packed关键字用于告诉编译器不要为结构体成员添加填充字节,从而使得结构体的大小更加紧凑。typedef关键字用于为数据类型定义一个新的名称。

下面是一个使用__packed和typedef的示例代码:

typedef __Packed struct
{
   
   
char c;
double d;
char e;
}
Packed_S;

则该结构体将只占用10个字节。

如果定义:
__packed关键字的意思是在struct和union结构中不添加填充字节:

 struct S
    {
   
   
        char
Build started: Project: SE71M *** Using Compiler 'V6.4', folder: 'C:\Keil_v5\ARM\ARMCLANG\Bin' Build target 'SE71M' warning: unknown warning option '-Wno-nonportable-include-path'; did you mean '-Wno-gnu-include-next'? [-Wunknown-warning-option] ../main/ex2_profile_hid_mouse.c(15): warning: In file included from... ../../../components/exble2/includes\ex2_api.h(18): warning: In file included from... ../../../components/exble2/includes/core_ble.h(18): warning: In file included from... ../../../components/exble2/includes/ble/ble_ll.h(85): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_header_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(85): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_header_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(98): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_data_header_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(98): error: expected ';' after top level declarator __PACKED_STRUCT _ble_data_header_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(117): error: use of '__PACKED' with tag type that does not match previous declaration struct __PACKED { ^~~~~~ union ../../../components/exble2/includes/ble/ble_ll.h(116): note: previous use is here typedef union __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(117): error: nested redefinition of '__PACKED' struct __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(116): note: previous definition is here typedef union __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(139): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(139): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(146): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_direct_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(146): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_direct_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(153): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_nonconn_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(153): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_nonconn_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(160): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(160): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(167): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_req_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(167): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_req_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(174): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_rsp_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(174): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_rsp_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(181): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_connect_ind_t { ^ fatal error: too many errors emitted, stopping now [-ferror-limit=] 1 warning and 20 errors generated. compiling ex2_profile_hid_mouse.c... warning: unknown warning option '-Wno-nonportable-include-path'; did you mean '-Wno-gnu-include-next'? [-Wunknown-warning-option] ../main/ex2_app_mouse.c(21): warning: In file included from... ../../../components/exble2/includes\ex2_api.h(18): warning: In file included from... ../../../components/exble2/includes/core_ble.h(18): warning: In file included from... ../../../components/exble2/includes/ble/ble_ll.h(85): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_header_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(85): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_header_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(98): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_data_header_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(98): error: expected ';' after top level declarator __PACKED_STRUCT _ble_data_header_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(117): error: use of '__PACKED' with tag type that does not match previous declaration struct __PACKED { ^~~~~~ union ../../../components/exble2/includes/ble/ble_ll.h(116): note: previous use is here typedef union __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(117): error: nested redefinition of '__PACKED' struct __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(116): note: previous definition is here typedef union __PACKED { ^ ../../../components/exble2/includes/ble/ble_ll.h(139): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(139): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(146): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_direct_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(146): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_direct_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(153): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_nonconn_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(153): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_nonconn_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(160): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_ind_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(160): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_ind_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(167): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_req_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(167): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_req_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(174): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_scan_rsp_t { ^ ../../../components/exble2/includes/ble/ble_ll.h(174): error: expected ';' after top level declarator __PACKED_STRUCT _ble_adv_scan_rsp_t { ^ ; ../../../components/exble2/includes/ble/ble_ll.h(181): error: unknown type name '__PACKED_STRUCT' __PACKED_STRUCT _ble_adv_connect_ind_t { ^ fatal error: too many errors emitted, stopping now [-ferror-limit=] 1 warning and 20 errors generated. compiling ex2_app_mouse.c... ../../../hal/device/om6239/GCC/startup_om6239.S(27): error: attribute name not recognised: Tag_ABI_align_preserved .eabi_attribute Tag_ABI_align_preserved, 1 ^ assembling startup_om6239.S... ".\out\SE71M.axf" - 39 Error(s), 2 Warning(s). Target not created. Build Time Elapsed: 00:00:00
最新发布
08-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢谢~谢先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值