设为首页 加入收藏

TOP

C++ 指针的偏移
2014-04-06 17:35:23 来源: 作者: 【 】 浏览:132
Tags:  指针

  C++ 指针的偏移

  A problem from my friend, what's the result of the following code:

  class str{

  public:

  int x;

  char s[0];

  };

  class foo{

  public:

  str * p;

  };

  int main() {

  foo f = {0};

  str* p1 = NULL;

  if (f.p->s){

  printf("%d\n", f.p->s);

  }

  return 0;

  }

  This sentence sets p to NULL:

  foo f = {0};

  f.p is a pointer of str, so f.p is an offset. I.e., f.p->s = base address + 4

  So the result is 4

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++ 中的处理类型 下一篇用BOOST BIND库提高C++性能

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·在 Redis 中如何查看 (2025-12-26 03:19:03)
·Redis在实际应用中, (2025-12-26 03:19:01)
·Redis配置中`require (2025-12-26 03:18:58)
·Asus Armoury Crate (2025-12-26 02:52:33)
·WindowsFX (LinuxFX) (2025-12-26 02:52:30)