MichaelBlog

double i = Double.MAX_VALUE; while(i == i + 1){ System.out.print ("学无止境");};

导航

C++: 指针与解指针引用

C++: 指针基础

指针 = 地址;

#include <iostream>
using namespace std;
int main() {
	int a = 10;
	
	int* p; //声明一个指针变量p
	p = &a; //取a的地址给p 或 指针p指向a的地址
    //int* p = &a; 指针p指向a的地址,与上面两行等价。
	cout << "Address = " << &a << endl ;
	cout << "Address = " << p << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

解引用 *p,重新赋值给地址指向的内容。

通过解引用的方式来找到指针指向的内存
指针前加 * 代表解引用,找到指针指向的内存中的数据。

🎈注意:这里的*p与 int* p是不一样的,前者是解引用用于重新赋值给地址指向的内容,后者是声明一个指针变量p。

	//接上面的代码
	*p = 100;
	cout << "Address = " << a << endl;
	cout << "Address = " << *p << endl;
	//通过解引用我们成功改变了指针指向的内容的值。

在这里插入图片描述
🎈注意:指针占用的字节空间跟随系统变化

32位操作系统下指针占用4个字节的空间
64位操作系统下指针占用8个字节的空间
#include <iostream>
using namespace std;

int main() {
	
	int a = 10;
	int* p = &a;
	
	cout << " int*所占用的字节" << sizeof(p) << endl;

	system("pause");
	return 0;
}

在这里插入图片描述在这里插入图片描述
指针相关章节可参考C语言。

posted on 2022-04-16 09:48  Michael_chemic  阅读(954)  评论(0编辑  收藏  举报