close

copy from 

http://stackoverflow.com/questions/3553296/c-sizeof-single-struct-member

 

 

Although defining the buffer size with a #define is one idiomatic way to do it, another would be to use a macro like this:

#define member_size(type, member) sizeof(((type *)0)->member)
and use it like this:

typedef struct
{
float calc;
char text[255];
int used;
} Parent;

typedef struct
{
char flag;
char text[member_size(Parent, text)];
int used;
} Child;
I'm actually a bit surprised that sizeof((type *)0)->member) is even allowed as a constant expression. Cool stuff.

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 阿肯 的頭像
    阿肯

    韌體開發筆記

    阿肯 發表在 痞客邦 留言(0) 人氣()