当前位置:网站首页>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;
}
// 整型数组 --存放整型
// 字符数组 --存放字符
// 指针数组 --存放指针边栏推荐
- 二叉树[全解](C语言)
- ORA-00257
- Kubernetes 网络入门
- day14--postman interface test
- oracle create tablespace
- ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExcep
- linux(centOs7)部署mysql(8.0.20)数据库
- 2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi
- DDOS攻击真的是无解吗?不!
- Is DDOS attack really unsolvable?Do not!
猜你喜欢

PCIe 核配置

第十四天&postman

缺陷检测(图像处理部分)

JWT简单介绍

【机器学习】21天挑战赛学习笔记(二)

如何发现一个有价值的 GameFi?

sqlite--nested exception is org.apache.ibatis.exceptions.PersistenceException:

面试汇总:为何大厂面试官总问 Framework 的底层原理?

Interview summary: Why do interviewers in large factories always ask about the underlying principles of Framework?

软件基础的理论
随机推荐
仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!
Exercise: Selecting a Structure (1)
Software testing interview questions: What are the seven-layer network protocols?
如何发现一个有价值的 GameFi?
深度学习:使用nanodet训练自己制作的数据集并测试模型,通俗易懂,适合小白
从一次数据库误操作开始了解MySQL日志【bin log、redo log、undo log】
如何用 Solidity 创建一个“Hello World”智能合约
【翻译】CNCF对OpenTracing项目的存档
Is DDOS attack really unsolvable?Do not!
LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
Creative code confession
Opencv——视频跳帧处理
创意代码表白
Interview summary: Why do interviewers in large factories always ask about the underlying principles of Framework?
GCC:头文件和库文件的路径
day14--postman接口测试
oracle create tablespace
阶段性测试完成后,你进行缺陷分析了么?
Why is this problem reported when installing oracle11
为什么他们选择和AI恋爱?