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

来源:互联网 发布:简述js闭包使用场景 编辑:程序博客网 时间:2024/06/06 01:43

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



2 0
原创粉丝点击