Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360172
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-18 21:36:40


  1. #include <stdio.h>

  2. #define FIND(type, f)    (size_t)&(((type *)0)->f)

  3. #define OFFSET(type, f)    ((size_t)((char *)&((type *)0)->f - (char *)(type *)0))

  4. struct _st
  5. {
  6.     int a;
  7.     char c;
  8.     int b;
  9. };

  10. int
  11. main(void)
  12. {
  13.     printf("%d\n", FIND(struct _st, a));
  14.     printf("%d\n", OFFSET(struct _st, a));

  15.     printf("%d\n", FIND(struct _st, c));
  16.     printf("%d\n", OFFSET(struct _st, c));

  17.     printf("%d\n", FIND(struct _st, b));
  18.     printf("%d\n", OFFSET(struct _st, b));

  19.     return (0);
  20. }



阅读(848) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

onezeroone2011-04-24 16:28:06

(type *)0