指针和参考运算符(&)

Van*_*nel 0 c++ pointers

我正在学习C++,我在一本书上发现了这个:

#include <iostream>
using namespace std;

int main()
{
int Age = 30;
int* pInteger = &Age; // pointer to an int, initialized to &Age

// Displaying the value of pointer
cout << “Integer Age is at: 0x” << hex << pInteger << endl;

return 0;
}
Run Code Online (Sandbox Code Playgroud)

书中说输出是存储Age的内存地址.

但是这本书没有谈到这个:

*pInteger = &Age;
 pInteger = &Age;
Run Code Online (Sandbox Code Playgroud)

这两项任务有什么区别?

Cas*_*Cow 6

你好像被这条线弄糊涂了

int* pInteger = &Age; // pointer to an int, initialized to &Age
Run Code Online (Sandbox Code Playgroud)

*这里的符号是将pInteger声明为指向int的指针.这被初始化(未分配)到Age允许的地址,因为Age是int.

你可以输入

*pInteger = 45;
Run Code Online (Sandbox Code Playgroud)

这将分配给pInteger指向的整数.

你也可以输入

int y = 35; 
pInteger = &y;
Run Code Online (Sandbox Code Playgroud)

这将指针重新分配指向不同的地方.