C++指针地址解释

标签 c++ pointers

我是 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的地址。

所以,mypointerfirstvalue的地址,&mypointermypointer的地址。由于 firstvaluemypointer 是不同的变量,因此它们具有不同的地址。

关于C++指针地址解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24756263/

相关文章:

c++ - 在 C 或 C++ (mac OSX) 中拥有 KeyDown 事件

c++ - 使用 IMFSourceReaderCallback 检测 USB 摄像头断开连接

c++ - 为什么使用 constexpr、__PRETTY_FUNCTION__ 和 char * 的这两段代码会有不同的结果?

c++ - 将 char 指针数组传递给函数

java - 我想找到图像的坐标

c++ - C 中的 sleep 函数错误

c++ - 一些 std::unique_ptr 使用和 "gotchas"

c - 每次更改其内容时都需要更改 char 指针的大小吗?

c++ - 调试 C++ 代码

计算地址 : pointer + non-negative number