当前位置:网站首页>指针(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;
}
边栏推荐
- 日志@Slf4j介绍使用及配置等级
- 把相亲角搬到海外,不愧是咱爸妈
- 机器学习实战(2)——端到端的机器学习项目
- Prada, big show?In the yuan in the universe that!
- Nanodlp v2.2/v3.0 light curing circuit board, connection method of mechanical switch/photoelectric switch/proximity switch and system state level setting
- Codeforces Round #276 (Div. 1) B. Maximum Value
- 大佬们有遇到过这个问题吗? MySQL 2.2 和 2.3-SNAPSHOT 都这样,貌似是
- es6-promise对象详解
- Jiugongge lottery animation
- 高数_证明_曲率公式
猜你喜欢
Prada, big show?In the yuan in the universe that!
Reversing words in a string in LeetCode
神经网络可视化有3D版本了,美到沦陷!(已开源)
Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer 论文笔记
矩阵键盘&基于51(UcosII)计算器小项目
3DS MAX batch export file script MAXScript with interface
机器学习实战(2)——端到端的机器学习项目
es6-promise对象详解
3DS MAX 批量导出文件脚本 MAXScript 带界面
11 + chrome advanced debugging skills, learn to direct efficiency increases by 666%
随机推荐
Redis上云迁移实践
【ECCV 2022|Millions of Prizes】PSG Competition: Pursuing the "Most Comprehensive" Scene Understanding
浙大、阿里提出DictBERT,字典描述知识增强的预训练语言模型
金山云要飘到哪里?
Stream通过findFirst()查找满足条件的一条数据
22家!北京昌平区通报存在食品安全问题餐饮服务企业
友邦人寿可观测体系设计与落地
LeetCode中等题之搜索二维矩阵
高数_证明_曲率公式
系统的安全和应用(不会点安全的东西你怎么睡得着?)
R语言实战应用案例:论文篇(一)-特殊柱形图绘制
LeetCode medium topic search of two-dimensional matrix
Codeforces Round #276 (Div. 1) D. Kindergarten
MySQL面试题整理
M²BEV: Multi-Camera Joint 3D Detection and Segmentation with Unified Bird’s-Eye View Representation
【百度统计】用户行为分析
Ethernet channel 以太信道
SenseTime self-developed robotic arm, the first product is an AI chess-playing robot: Guo Jingjing is also invited as an endorsement
没有接班人,格力只剩“明珠精选”
Cloud Migration Practice of Redis