我是 C++ 的新手,我有一段这样的代码:
int firstvalue=10;
int * mypointer;
mypointer = &firstvalue;
cout << "pointer is " << *mypointer << '\n';
cout << "pointer is " << mypointer << '\n';
cout << "pointer is " << &mypointer << '\n';
结果是:
pointer is 10
pointer is 0x7ffff8073cb4
pointer is 0x7ffff8073cb8
谁能给我解释一下为什么“mypointer”和“&mypointer”的结果不同?
非常感谢。
最佳答案
mypointer
是变量mypointer
的值。由于您的分配,该值是firstvalue
的地址。&mypointer
是变量mypointer
的地址。即mypointer
的地址。
所以,mypointer
是firstvalue
的地址,&mypointer
是mypointer
的地址。由于 firstvalue
和 mypointer
是不同的变量,因此它们具有不同的地址。
关于C++指针地址解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24756263/