当前位置:网站首页>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;
}
// 整型数组 --存放整型
// 字符数组 --存放字符
// 指针数组 --存放指针边栏推荐
- [How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?
- The principle of NMS and its code realization
- 蓝牙Mesh系统开发四 ble mesh网关节点管理
- Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!
- Use of pytorch: Convolutional Neural Network Module
- torch.autograd.grad求二阶导数
- Method Overriding and Object Class
- Software testing interview questions: the difference and connection between black box testing, white box testing, and unit testing, integration testing, system testing, and acceptance testing?
- Is DDOS attack really unsolvable?Do not!
- GCC:头文件和库文件的路径
猜你喜欢

Creative code confession

After the staged testing is complete, have you performed defect analysis?

oracle create user

Three handshake and four wave in tcp

If capturable=False, state_steps should not be CUDA tensors

10年测试经验,在35岁的生理年龄面前,一文不值

Gartner Hype Cycle:超融合技术将在2年内到达“生产力成熟期”

活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!

OPENWIFI实践1:下载并编译SDRPi的HDL源码

内存取证系列1
随机推荐
行业现状?互联网公司为什么宁愿花20k招人,也不愿涨薪留住老员工~
Day Fourteen & Postman
Opencv - video frame skipping processing
[Machine Learning] 21-day Challenge Study Notes (2)
Dynamic Programming/Knapsack Problem Summary/Summary - 01 Knapsack, Complete Knapsack
【翻译】CNCF对OpenTracing项目的存档
Pytorch使用和技巧
2021年11月网络规划设计师上午题知识点(下)
(十七)51单片机——AD/DA转换
BC(转)[js]js计算两个时间相差天数
码率vs.分辨率,哪一个更重要?
Software testing interview questions: What are the seven-layer network protocols?
【七夕如何根据情侣倾听的音乐进行薅羊毛】背景音乐是否会影响情侣对酒的选择
Software testing interview questions: the difference and connection between black box testing, white box testing, and unit testing, integration testing, system testing, and acceptance testing?
Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 2)
习题:选择结构(一)
主库预警日志报错ORA-00270
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
硬实力和软实力,哪个对测试人来说更重要?
多线程涉及的其它知识(死锁(等待唤醒机制),内存可见性问题以及定时器)