当前位置:网站首页>指针(C语言初解)
指针(C语言初解)
2022-08-10 13:01:00 【叽里呱啦呱】
在计算机科学中,指针是编程语言的一个对象,利用地址,它的值直接指向存在电脑储存器中另一个地方的值,通过地址可以找到所需的变量单元,可以说地址指向该变量单元,因此形象的称为“地址”。意思是可以找到以它为地址的内存单元。
#include<stdio.h>
int main() {
printf("%d\n",sizeof(char*));
printf("%d\n", sizeof(int*));
printf("%d\n", sizeof(double*));
printf("%d\n", sizeof(short*));
return 0;
}
*证明 指针占四个字节
*取值符 &取地址符
- 指针类型决定了指针进行解引用的时候,能够访问空间的大小,决定了指针变量指向的类型就是啥类型
int 4字节
char 1字节
double 8 字节
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>int main() {
int a[10] = {0};
int* p=a ;//数组名-首元素的地址
int i=0;
for (i = 0; i < 10; i++) {
*(p + i)=1;
}
printf("%d",a[9]);
return 0;
}
- 野指针(指针指向的位置是不可知的)
成因:1,指针未初始化
2,指针越界访问
int a[]={0};
int *p=a;
int i=0;
for(i=0;i<11,1++){
p++;
}
3,调用函数里的地址返回系统
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int *test() {
int a = 10;
return &a;
}
int main() {
int* p = test();//函数调用完之后就自动注销分配的10的地址了,*p就成了野指针了
*p = 20;
return 0;
}
*p=NULL指针为空的时候 不能访问
- 指针运算
1,指针+整数
2,指针-整数
3,指针的关系运算
指针+-整数:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main() {
int a[10] = { 1,2,3,4,6,7,8,9,10 };
int* p = a;
int s = sizeof(a)/sizeof(a[0]),i=0;
for (i = 0; i < s; i++) {
printf("%d\n", *p);
p=p+1;
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define arr 2
int main() {
int v[arr];
int* p;
int i = 0;
for (p = &v[0]; p <&v[arr];) {
*p++ = 0;
}
for (i = 0; i < 2; i++) {
printf("%d", v[i]);
}
return 0;
}
大地址-小地址得到的是中间元素的个数+1,两个不同数组地址相减的时候结果不可预知
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define arr 2
int main() {
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int* p,b;
b=&a[9] - &a[0];
printf("%d", b);
return 0;
}
边栏推荐
- Guidelines for Sending Overseas Mail (2)
- Code Casual Recording Notes_Dynamic Programming_70 Climbing Stairs
- 【iOS】面试整理
- 【mysql索引实现原理】
- 学习日记8
- 鸿蒙开发从hello world开始
- 进程和计划任务管理
- 递归递推之计算组合数
- 神经网络可视化有3D版本了,美到沦陷!(已开源)
- Keithley DMM7510 accurate measurement of ultra-low power consumption equipment all kinds of operation mode power consumption
猜你喜欢
![ArcMAP has a problem of -15 and cannot be accessed [Provide your license server administrator with the following information:Err-15]](/img/da/b49d7ba845c351cefc4efc174de995.png)
ArcMAP has a problem of -15 and cannot be accessed [Provide your license server administrator with the following information:Err-15]

mSystems | Zhongnong Wang Jie Group Reveals the Mechanisms Affecting Soil "Plastic Interstitial" Microbial Communities

【百度统计】用户行为分析

接口自动化测试基础篇

浙大、阿里提出DictBERT,字典描述知识增强的预训练语言模型

【ECCV 2022|百万奖金】PSG大赛:追求“最全面”的场景理解

友邦人寿可观测体系设计与落地

Wirshark common operations and tcp three-way handshake process example analysis

2022 Recruitment Notice for Academician Zhao Guoping Group of Shenzhen Institute of Advanced Technology, Chinese Academy of Sciences

数字藏品,“赌”字当头
随机推荐
[Study Notes] Persistence of Redis
领域驱动实践总结(基本理论总结与分析V+架构分析与代码设计+具体应用设计分析)
Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer Paper Notes
Redis 定长队列的探索和实践
作业8.9 构建TCP协议的服务器
shell:常用小工具(sort、uniq、tr、cut)
【mysql索引实现原理】
Jenkins修改默认主目录
Import other custom namespaces in C#
学习日记9
高数_证明_曲率公式
Code Casual Recording Notes_Dynamic Programming_70 Climbing Stairs
Reversing words in a string in LeetCode
【iOS】面试整理
Digicert EV证书签名后出现“证书对于请求用法无效”的解决方案
The recursive recursive Fighting_ silver study ah but level 4
生成树协议STP(Spanning Tree Protocol)
一个 CRM One Order Application log 的单元测试报表
Comparison version number of middle questions in LeetCode
Redis上云迁移实践