9-指针的读/写操作

代码实现

#include <iostream>
using namespace std;

int main() {
	int n = 1;
	float f = 1.0;
	double d = 2.0;
	char c = '*';
	int* pn = &n;
	float* pf = &f;
	double* pd = &d;
	char* pc = &c;
	cout << "&n: " << &n << endl;
	cout << "&f: " << &f << endl;
	cout << "&d: " << &d << endl;
	cout << "&c: " << &c << endl;

	cout << "pn: " << pn << endl;
	cout << "pf: " << pf << endl;
	cout << "pd: " << pd << endl;
	cout << "pc: " << pc << endl;

	cout << "&pn: " << &pn << endl;
	cout << "&pf: " << &pf << endl;
	cout << "&pd: " << &pd << endl;
	cout << "&pc: " << &pc << endl;

	cout << "n: " << n << endl;
	cout << "f: " << f << endl;
	cout << "d: " << d << endl;
	cout << "c: " << c << endl;

	*pn = 999; // 对pn存储的地址值所对应的内存单元进行赋值操作
	*pf = 888;
	*pd = 777;
	*pc = 'A';

//  *pn为从pn中存储的地址值所对应的内存单元中,读取内存单元的内容
	cout << "n: " << n << " *pn = " << *pn << endl;
	cout << "f: " << f << " *pf = " << *pf << endl;
	cout << "d: " << d << " *pd = " << *pd << endl;
	cout << "c: " << c << " *pc = " << *pc << endl;
	return 0;
}

运行结果

image

图解一

image

图解二

image

posted @ 2022-11-20 15:11  彩红caihong  阅读(26)  评论(0)    收藏  举报