参考:https://www.cnblogs.com/sggggr/p/15028904.html

如上宏的意义,获取结构体S中元素M,相对于首元素的偏移量。

#include <stdio.h>
#define STRUCT_OFFSET(s,m)   ( (int)(&((s*)0)->m)) /* 定义获取结构成员相对偏移地址的宏 */
 
struct man
{
	char name[63];
	int year;
	char son_name[59];
	char son_year;
};
int main()
{ 
    printf("name offset:%d\n",STRUCT_OFFSET( struct man, name ));
    printf("year offset:%d\n",STRUCT_OFFSET( struct man, year ));
	printf("son_name offset:%d\n",STRUCT_OFFSET( struct man, son_name ));
	printf("son_year offset:%d\n",STRUCT_OFFSET( struct man, son_year ));
    return 0;
}

程序结果:

name offset:0
year offset:64
son_name offset:68
son_year offset:127

相关文章:

  • 2021-06-12
  • 2021-07-27
  • 2022-12-23
  • 2021-11-10
  • 2021-10-08
  • 2022-12-23
  • 2022-02-10
  • 2021-12-12
猜你喜欢
  • 2022-12-23
  • 2021-07-13
  • 2022-12-23
  • 2022-12-23
  • 2021-06-01
  • 2021-12-18
  • 2021-10-14
相关资源
相似解决方案