C++ 这个多重间接寻址是如何工作的?
我期待着-C++ 这个多重间接寻址是如何工作的?,c++,pointers,C++,Pointers,我期待着- 30 20 30 我不知道在这种情况下多重间接寻址是如何工作的 由于该语句,行cout变量ipp指向变量ip1 ipp=&ip1; // make ipp point to ip1 *ipp=ip2; // dereference on ipp, change the value of pointee (i.e. ip1) to ip2 // i.e. make ip1 point to j *ipp=&a
30
20
30
我不知道在这种情况下多重间接寻址是如何工作的
由于该语句,行cout变量
ipp
指向变量ip1
ipp=&ip1; // make ipp point to ip1
*ipp=ip2; // dereference on ipp, change the value of pointee (i.e. ip1) to ip2
// i.e. make ip1 point to j
*ipp=&k; // change the value of pointee (i.e. ip1) to the address of k
// i.e. make ip1 point to k
cout<<*ip1<<endl; // now we get 30 (i.e. the value of k)
ipp=&ip1;
ip1 = ip2;
因此,对指针ipp
的任何解引用都会产生指针ip1
的值。例如,这句话
ipp=&ip1; // make ipp point to ip1
*ipp=ip2; // dereference on ipp, change the value of pointee (i.e. ip1) to ip2
// i.e. make ip1 point to j
*ipp=&k; // change the value of pointee (i.e. ip1) to the address of k
// i.e. make ip1 point to k
cout<<*ip1<<endl; // now we get 30 (i.e. the value of k)
ipp=&ip1;
ip1 = ip2;
相当于
*ipp=ip2;
*ipp=&k;
这句话呢
ipp=&ip1; // make ipp point to ip1
*ipp=ip2; // dereference on ipp, change the value of pointee (i.e. ip1) to ip2
// i.e. make ip1 point to j
*ipp=&k; // change the value of pointee (i.e. ip1) to the address of k
// i.e. make ip1 point to k
cout<<*ip1<<endl; // now we get 30 (i.e. the value of k)
ipp=&ip1;
ip1 = ip2;
相当于
*ipp=ip2;
*ipp=&k;
因此,ip1
包含变量k
的地址,而ipp
包含ip1
本身的地址
这些声明
ip1 = &k;
coutif如果最后两个输出是清楚的,为什么不删除它们?这会使问题更容易阅读。我想这样理解问题会更容易。@AseemBhardwaj是的,如果你不完全理解这些陈述,可能很难确定哪些部分是必要的,哪些部分不是。无论如何,我想你现在可以决定了是的。谢谢你,先生!;)谢谢一件事,如果我们省略*ipp=ip2;输出仍然是一样的,那行没有必要吗?@AseemBhardwaj是的,可能是这样,因为*ipp=ip2代码>被*ipp=&k代码>立即。再次感谢。我也在想同样的事情。