问一个关于宏的问题
如何定义一个宏,求出给定结构中给定成员的偏移量#define OFFSET(structure, member) ??
取其字节偏移是可以的:
#define OFFSET(structure, member) ( (unsigned int) &(structure.member) - (unsigned int) &(structure) )
例:
typedef struct
{
int a;
char b;
float c;
double d;
}Temp;
int main(void)
{
Temp t;
printf("%u\n", &(t) );
printf("%u\n", OFFSET(t, a) );
printf("%u\n", OFFSET(t, b) );
printf("%u\n", OFFSET(t, c) );
printf("%u\n", OFFSET(t, d) );
getch();
return 0;
}
[此贴子已经被作者于2007-9-16 1:32:12编辑过]
#pragma pack(1)
typedef struct
{
int a;
char b;
float c;
double d;
}Temp;
#pragma pack()