C++ 指针的偏移 The offset of a pointer in 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++ 指针的偏移 The offset of a pointer in C++,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。