能否在编译时计算所需的填充数组大小,以使结构体最终达到预定大小?也就是说,我想做这样的事情(虽然知道不会起作用)。
#define APPDESCTARGETSIZE 256 // Target size for the App Desc Structure
// Structure member size of
#define msizeof(type, member) sizeof(((type *)0)->member)
// Size of padding arrary
#define padsize (APPDESCTARGETSIZE - (offsetof(custom_app_desc_t, pad) + msizeof(custom_app_desc_t, tail)))
/** * @brief Description about application. */
typedef struct {
uint8_t header[16];
char progname[16];
char boardname[16];
uint8_t chipsize;
uint8_t version_maj;
uint8_t version_min;
uint8_t version_iss;
uint8_t pad[padsize];
uint8_t tail[16];
} custom_app_desc_t;
我可以通过手动计算来解决,例如下面的方式,但编译器为我执行数学计算会更好:
#define padsize (APPDESCTARGETSIZE - 68)
注意,在这种情况下,重要的是填充在“tail”成员之前发生。