首页 > 编程语言 > 详细

C++ 指针的偏移 The offset of a pointer in C++

时间:2014-04-02 05:03:39      阅读:585      评论:0      收藏:0      [点我收藏+]

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++ 指针的偏移 The offset of a pointer in C++,布布扣,bubuko.com

C++ 指针的偏移 The offset of a pointer in C++

原文:http://blog.csdn.net/taoqick/article/details/22760129

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!