设为首页 加入收藏

TOP

C++ 指针的偏移 The offset of a pointer in C++
2014-04-07 00:35:49 来源: 作者: 【 】 浏览:126
Tags:指针 The offset pointer

  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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇用LuaBridge为Lua绑定C/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)