【发布时间】:2021-09-25 15:54:55
【问题描述】:
为什么一个指针包含两个内存位置?第二个内存位置的用例是什么?
#include <iostream>
using std::cout;
int main()
{
string animal;
string *rabbit = &animal;
cout << rabbit << " 1st memory location \n"
<< &rabbit << " 2nd memory location";
return 0;
}
【问题讨论】:
-
你需要一个物理的地方来存储指针本身。它不是“免费的”。
-
你会说
animal包含一个字符串和一个内存位置吗?您可以轻松地输出animal和&animal,其中第二个输出是内存位置。这是否意味着animal“持有”一个内存位置?