指针的初步介绍

指针在c++中算是比较复杂的一部分,这篇文章主要写的是指针的的定义

 *是用来定义指针

&是用来查询地址

#include<cmath>
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int a = 1000;
	int *p;
	p = &a;
	cout<<&a<<endl;
	cout<<p<<endl;
}

 运行结果:

0x6ffe14
0x6ffe14
 

#include<cmath>
#include<iostream>
using namespace std;
int main()
{
	int a = 1001;
	int *p;
	p  = &a;
	cout<<a<<endl;
	cout<<&a<<endl;
	cout<<p<<endl;
	return 0;
	
}

运行结果:

1001
0x6ffe14
0x6ffe14

我们再来看数组的地址是怎么样的

#include<cmath>
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int *p;
	int b = 1;
	int a[1000];
	for(int i=0;i<20;i++)
	{
		a[i] = b;
		p = &a[i];
		cout<<p<<endl;
		 
	}
	return 0;
}

 运行结果:

0x6fee70
0x6fee74
0x6fee78
0x6fee7c
0x6fee80
0x6fee84
0x6fee88
0x6fee8c
0x6fee90
0x6fee94
0x6fee98
0x6fee9c
0x6feea0
0x6feea4
0x6feea8
0x6feeac
0x6feeb0
0x6feeb4
0x6feeb8
0x6feebc

#include<cmath>
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	double *p;
	double b = 1;
	double a[1000];
	for(int i=0;i<20;i++)
	{
		a[i] = b;
		p = &a[i];
		cout<<p<<endl;
		 
	}
	return 0;
}

.

运行结果:

0x6fdec0
0x6fdec8
0x6fded0
0x6fded8
0x6fdee0
0x6fdee8
0x6fdef0
0x6fdef8
0x6fdf00
0x6fdf08
0x6fdf10
0x6fdf18
0x6fdf20
0x6fdf28
0x6fdf30
0x6fdf38
0x6fdf40
0x6fdf48
0x6fdf50
0x6fdf58

通过上面代码演示我们可以发现在一个数组中地址会根据类型大小去排列

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值