在下面的代码中,基于阅读cplusplus.com,我试图测试我对指针的基本理解。
#include <iostream>
using namespace std;
int main() {
int foo, *bar, fubar;
foo = 81;
bar = &foo;
fubar = *bar;
cout << "the value of foo is " << foo << endl;
cout << "the value of &foo is " << &foo << endl;
cout << "the value of bar is " << bar << endl;
cout << "the value of *bar is " << *bar << endl;
cout << "the value of fubar is " << fubar << endl;
cin.get();
}
这导致输出:
the value of foo is 81
the value of &foo is xx
the value of bar is xx
the value of *bar is 81
the value of fubar is 81
xx
每次运行时都会发生变化的长数字在哪里。当我添加以下内容时:
cout << "the address of foo is " << &foo << endl;
cout << "the address of fubar is " << &fubar << endl;
它导致:
the address of foo is xx
the address of fubar is xy
与运行时xy
不同的地方。xx
问题1:在声明中,声明
*bar
是否在那个时刻使它成为一个“指针”,直到它被使用,即fubar = *bar
在什么时候它是一个取消引用的变量?或者是指针始终是一个变量,而这只是我得到了所有的nooby并被束缚在(可能不正确的)语义中?问题2:
xx
每个运行时都会改变一个长数字,所以我说它是地址空间是对的吗?问题 3:我是否认为虽然
fubar
和foo
具有相同的值,但它们是完全独立的并且没有公共地址空间?