当前位置:网站首页>初级指针~带你入门指针
初级指针~带你入门指针
2022-06-09 08:51:00 【一般路过半缘君】
什么是指针?
指针,是在内存之中最小单元的编号,也就是地址,而我们平时口头说的指针实际上是指针变量,也就是用来存储地址的变量。
当我们用sizeof来计算指针变量大小的时候,会发现,无论什么类型的指针变量,大小都是一样的,32位平台 是4,64位平台是8,这里可能就会引起大家的疑惑,既然每一个指针变量的大小一样,那么不同类型的指针有什么用呢?其实啊,指针的类型是决定指针进行计算的时候会修改或者变化几个字节。
1:指针类型决定指针变量解引用的时候,修改的字节数
比如int*是四个字节,char*是一个字节。
我们给出下面这个实列让大家更快的理解:
#include<stdio.h>
int main()
{
int a = 0x11223344;
char *pc = (char*)&a;
*pc = 0;
printf("%x",a);
return 0;
}
我们能够看到,只有44被改成了00,这是因为4个字节存储的进制为16进制,而a中每两个数字存在一个字节里面,又pc是char*类型的,只能访问一个字节,因此a被更改成了11223300;
2:指针类型又决定了指针进行加减操作的时候,能够跳过几个字节
比如int*类型的指针变量,当他+1获知-1就会增加4个字节或者减少4个字节;
但是指针还有一个问题,那就是野指针问题;
野指针
野指针通常由以下几个问题引起:
1:创建的指针变量而未初始化
2:指针变量指向的范围超过了数组范围,成为了野指针
3:指针指向的地址是函数内部的变量地址
只有第三点比较难以理解,为了方便大家理解,这里给出一个简单的列子:
#include<stdio.h>
int* test()
{
int a = 10;
return &a;
}
int main()
{
int* p = test();
return 0;
}这里p指针变量指向了函数内a的地址,但是我们都知道,函数在使用完后其内部变量会被销毁,因此p变量指向的地址没了,于是p成了野指针。
这里给出几个避免野指针的技巧:
1:指针初始化时,若没有指向的变量,就初始化为NULL
2:指针只有存储了一个有效的地址的时候才能够解引用
3:避免用指针指向局部变量的地址
4:使用指针先检查有效性
指针运算
指针变量也是变量,那么指针当然也有它自己的运算方式,
1:指针加减整数
指针加减整数就会移动对应字节的地址大小,我们可以用这种方式直接访问数组。
比如:
#include<stdio.h>
int main()
{
int arr[10] = { 0 };
int* p = arr;
for (int i = 0; i < 10; i++)
{
*(p + i) = 1;
}
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
} 
结果如上。
2:指针减去指针
指针减去指针的前提是两个指针指向的是同一片空间,并且两个指针相减的绝对值为两个元素个数。
#include<stdio.h>
int main()
{
int arr[10] = { 0 };
int* p1 = arr;
int* p2 = (arr + 10);
printf("%d", p2 - p1);
return 0;
} 
3:指针关系运算(比较大小)
前提也是两个指针必须指向同一块空间;
但是有一个需要注意的是,在标准中规定了允许指向数组元素的指针与指向数组最后一个元素后一个元素的地址比较,但不允许与指向数组第一个元素之前的一个元素的地址比较
而和指针具有紧密关系的还有就是数组了,数组的元素名实际上就是数组首元素的地址,因此可以利用指针来访问数组,至于如何访问,之前就已经展示过来就不再展示。
之前我们讲的都是一级指针,当然我们还要二级指针,二级指针的概念挺简单,就是储存一级指针地址的指针。
而指针变量也可以用数组存储,一个全部是指针变量的数组就是指针数组;
指针数组
指针数组形式如下
int * a[10];
a数组存储的都是int*类型的变量。
边栏推荐
- [texstudio] [1] fractional dfrac, tfrac undefined control square error
- MySQL基础 子查询练习
- 85. (leaflet house) leaflet military plotting - line arrow drawing
- Remote prepayment management system helps property management solve the problem of difficult charging and statistics
- Precautions for Tencent cloud pagoda website construction
- I met a big man!
- MySQL uses while to batch insert data in stored procedures (performance difference between batch submission and single submission)
- ESP32学习笔记【WiFi网络篇】-01AP&STA
- [redis learning 11] data persistence of distributed cache, master-slave cluster
- Acwing第 54 场周赛
猜你喜欢

Qt development -- compilation of serial port assistant

3D編程模式:依賴隔離模式

附十七章 网络程序解读限定文章

Creation of JS class and use of constructor constructor
![[texstudio] [2] general picture and table presentation](/img/1e/bb9488984cb1a3b233978d25f83b6e.png)
[texstudio] [2] general picture and table presentation

清洗数据---2022/06/08

Modify campaign table

The application of the comprehensive energy efficiency management system of ankery in the data center
![[antenna] [2] explanation of some nouns and simple concepts, still](/img/84/7fb2b01dc717eb9196b33066ea3b4e.png)
[antenna] [2] explanation of some nouns and simple concepts, still

MySQL基础 多表查询
随机推荐
3D programming mode: dependent isolation mode
防火门监控系统对防火门状态进行24小时实时自动巡检
RMAN backup concept_ About RMAN incremental backup
MySQL基础 基础认知
Remote prepayment management system helps property management solve the problem of difficult charging and statistics
Direct application of 10 kinds of question type explosive text title sentence patterns
如何解决mouseup事件失效的问题
安装mysql保姆级教程
mysql常用命令
Do you want to explore MySQL not in indexing?
项目面试题
How to view websites or apps bound to mobile phones: what you must do before you log off your mobile phone number - change and bind all websites or apps bound to your old mobile phone number
Codeworks round 797 (Div. 3) a~e problem solving record
FreeRTOS task notification review
配置Conda环境和Pytorch安装
How to use alicloud CDN to cache static websites deployed on function computing
Wechat applet to obtain basic user information
Update and delete operations in Clickhouse of data warehouse series
2022-2028 global UAV detection and jamming system industry survey and trend analysis report
I met a big man!