C++ 访问结构的char成员变量地址

C++ 访问结构的char成员变量地址,c++,memory-management,C++,Memory Management,我有一个结构,我试图打印他们的成员变量的地址。 当试图通过&f.c打印char成员变量的地址时,我没有得到他们的地址 代码如下: 我只是想知道,当我试图通过&f.c.访问它时,为什么它没有打印出来 使用gcc版本3.4.6编译的cout有一个运算符cout有一个运算符原因是没有重新解释cast&f.c是一个char*指针,cout将其视为字符串。因为您没有用任何内容填充char,所以您调用了一个未定义的行为,即它可以打印任何内容。原因是没有重新解释cast&f.c是char*指针,cout将其视

我有一个结构,我试图打印他们的成员变量的地址。 当试图通过&f.c打印char成员变量的地址时,我没有得到他们的地址

代码如下: 我只是想知道,当我试图通过&f.c.访问它时,为什么它没有打印出来


使用gcc版本3.4.6编译的cout有一个运算符cout有一个运算符原因是没有重新解释cast&f.c是一个char*指针,cout将其视为字符串。因为您没有用任何内容填充char,所以您调用了一个未定义的行为,即它可以打印任何内容。

原因是没有重新解释cast&f.c是char*指针,cout将其视为字符串。因为您没有用任何东西填充字符,所以您调用了一个未定义的行为,即它可以打印任何东西

struct foo
{
        char c;
        short s;
        void *p;
        int i;
};

int main()
{
        cout << "Size of foo: " << sizeof(foo) << endl;

        foo f;
        cout << "Address of c: " << reinterpret_cast<void*>(&f.c) << endl;
        cout << "Address of c: " << &(f.c) << endl;
        cout << "Address of s: " << reinterpret_cast<void*>(&f.s) << endl;
        cout << "Address of s: " << &(f.s) << endl;
        cout << "Address of p: " << reinterpret_cast<void*>(&f.p) << endl;
        cout << "Address of p: " << &(f.p) << endl;
        cout << "Address of i: " << reinterpret_cast<void*>(&f.i) << endl;
        cout << "Address of i: " << &(f.i) << endl;


        return 1;
}
/pp/cplus/bas ]$ ./a.out 
Size of foo: 12
Address of c: 0xffbfe680
Address of c:                   //----------- &(f.c). Why this is empty.. 
Address of s: 0xffbfe682
Address of s: 0xffbfe682
Address of p: 0xffbfe684
Address of p: 0xffbfe684
Address of i: 0xffbfe688
Address of i: 0xffbfe688