当前位置:网站首页>C语言基础知识 -- 指针
C语言基础知识 -- 指针
2022-08-05 01:15:00 【稚子】
1.指针是什么
在计算机科学中,指针(pointer)是编程语言中的一个对象,利用地址,它的值直接指向存在电脑存储器中另一个地方的值。由于通过地址能找到所需的变量单元,可以说,地址指向该变量单元。因此,将地址形象化的称为“指针”,意思是通过它能找到以它为地址的内存单元。
指针是一个变量,内存存放单元的地址(编号)
一个单元为1个字节
总结:
- 指针是用来存放地址的,地址是唯一标示一块地址空间的
- 指针的大小在32位平台是4个字节,在64位平台是8个字节
- 1个字节8位
#include <stdio.h>
int main()
{
int a = 10;
int *p = &a;
return 0;
}
2.指针和指针类型
#include <stdio.h>
int main()
{
printf("%d\n",sizeof(char*));
printf("%d\n",sizeof(short*));
printf("%d\n",sizeof(int*));
printf("%d\n",sizeof(double*));
return 0;
}
// 指针类型,在64位平台,输出结果均为8
指针类型 决定了指针进行解引用操作的时候,能够访问空间的大小
指针类型决定了:指针向前或向后走一步有多大距离
int *p; //*p能够访问4个字节
char *p; //*p能够访问1个字节
double *p; //*p能够访问8个字节
int *p+1; //*p跳过4个字节
char *p+1; //*p跳过1个字节
double *p+1; //*p跳过8个字节
通过指针把数组中每个元素都变成1(重要,值得参考)
#include <stdio.h>
int main()
{
int arr[10] = {0};
int *p = arr; // 数组名 -- 首元素的地址
int i = 0;
for(i = 0;i<10;i++){
*(p+i) = 1; // 把数组中的每个元素换成1
}
return 0;
}
总结:指针的类型决定了,对指针解引用的时候有多大的权限(能操作几个字节),比如:char*的指针解引用就只能访问一个字节,而int*的指针的解引用就能访问4个字节。
3.野指针
int a; // 局部变量不初始化,默认是随机值
int *p; // 局部的指针变量,就被初始化随机值
指针变量未初始化、指针越界访问、指针指向的空间被释放(取局部变量的地址),都会产生野指针
3.1 如何规避野指针
1.指针初始化
#include <stdio.h>
int main()
{
int b = 0;
int a = 10;
int *p = &a; // 初始化
int *p = NULL; // NULL -- 用来初始化指针,给指针赋值
return 0;
}
2.小心指针越界
3.指针指向空间释放,即使其置NULL
4.指针使用之前检查有效性
4.指针运算
- 指针+-整数
- 指针-指针
得到的为中间元素的个数
- 指针的关系运算
指针可以比较大小
5.指针和数组
数组名为首元素的地址
#include <stdio.h>
int main()
{
int arr[10] = {0};
printf("%p\n", arr);
printf("%p\n", &arr[0]);
printf("%p\n", &arr);
return 0;
}
// 1. &arr --&数组名,数组名不是首元素的地址,数组名表示整个数组
// &数组名,取出来的是整个数组的地址
// 2. sizeof(arr) -- sizeof(数组名)--数组名表示的整个数组
// --sizeof(数组名)计算的是整个数组的大小
6.二级指针
#include <stdio.h>
int main()
{
int a = 10;
int *pa = &a;
int **ppa = &pa; // 二级指针
return 0;
}
7.指针数组
存放指针的数组,本质为一个数组
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int c = 30;
int *arr[3] = {&a, &b, &c};
int i = 0;
for (i = 0; i < 3; i++)
{
printf("%d\n", *(arr[i]));
}
return 0;
}
// 整型数组 --存放整型
// 字符数组 --存放字符
// 指针数组 --存放指针
边栏推荐
- Exercise: Selecting a Structure (1)
- Software testing interview questions: What are the seven-layer network protocols?
- 主库预警日志报错ORA-00270
- 动态规划/背包问题总结/小结——01背包、完全背包
- Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!
- 2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi
- IJCAI2022 | DictBert:采用对比学习的字典描述知识增强的预训练语言模型
- 快速批量修改VOC格式数据集标签的文件名,即快速批量修改.xml文件名
- 4. PCIe interface timing
- [Machine Learning] 21-day Challenge Study Notes (2)
猜你喜欢
张驰咨询:揭晓六西格玛管理(6 Sigma)长盛不衰的秘密
JUC thread pool (1): FutureTask use
Interview summary: Why do interviewers in large factories always ask about the underlying principles of Framework?
Binary tree [full solution] (C language)
After the staged testing is complete, have you performed defect analysis?
面试汇总:为何大厂面试官总问 Framework 的底层原理?
新唐NUC980使用记录:在用户应用中使用GPIO
oracle create user
硬实力和软实力,哪个对测试人来说更重要?
linux(centOs7)部署mysql(8.0.20)数据库
随机推荐
Jin Jiu Yin Shi Interview and Job-hopping Season; Are You Ready?
Inter-process communication and inter-thread communication
金仓数据库 KingbaseES V8 GIS数据迁移方案(3. 基于ArcGIS平台的数据迁移到KES)
硬实力和软实力,哪个对测试人来说更重要?
CNI(Container Network Plugin)
Object.defineProperty实时监听数据变化并更新页面
10年测试经验,在35岁的生理年龄面前,一文不值
Software Testing Interview Questions: What do you think about software process improvement? Is there something that needs improvement in the enterprise you have worked for? What do you expect the idea
深度学习:使用nanodet训练自己制作的数据集并测试模型,通俗易懂,适合小白
从一次数据库误操作开始了解MySQL日志【bin log、redo log、undo log】
Software testing interview questions: Please draw the seven-layer network structure diagram of OSI and the four-layer structure diagram of TCP/IP?
新来个技术总监,把DDD落地的那叫一个高级,服气
oracle create tablespace
ORA-01105 ORA-03175
A new technical director, who calls DDD a senior, is convinced
软件测试技术之最有效的七大性能测试技术
2021年11月网络规划设计师上午题知识点(下)
阶段性测试完成后,你进行缺陷分析了么?
Lattice PCIe 学习 1
matlab 采用描点法进行数据模拟和仿真