获取指向指针的指针元素(c++)
这是我写的代码:获取指向指针的指针元素(c++),c++,pointers,C++,Pointers,这是我写的代码: #include <iostream> using namespace std; struct working{ int*pointer; int inf; }; int main() { working one; working *two; working **three; one.inf=5; one.pointer=NULL; two=&one; cout<<two<<endl;
#include <iostream>
using namespace std;
struct working{
int*pointer;
int inf;
};
int main()
{
working one;
working *two;
working **three;
one.inf=5;
one.pointer=NULL;
two=&one;
cout<<two<<endl;
cout<<two->pointer<<endl;
cout<<two->inf<<endl;
three=&two;
cout<<three<<endl;
cout<<three->pointer<<endl;
return 0;
}
如何以与使用两个元素类似的方式从三个元素获取inf或pointer元素?
如果可能的话,你能告诉我*three=two是做什么的吗?如果我把它换成three=&two,为什么它不编译呢?*three=two是做什么的。为什么它不编译呢?那会编译吗?但它不会工作,运行时可能会崩溃。你应该得到一个编译警告,虽然这可能有助于你理解它-如果你没有打开警告。箭头操作符->是一个指针取消引用,后跟成员访问。在您的示例中,two->pointer等于*two.pointer。现在,在three->pointer的上下文中考虑一下这个问题。这个问题表明,你没有努力去弄清楚指针是如何工作的,也没有尝试用谷歌搜索一下指针在C++中是如何工作的。谢谢你的回答。
three = &two;
cout << (*three) << endl;
cout << (*three)->pointer << endl;