当前位置:网站首页>指针(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;
}
边栏推荐
- 汉字检测和关键词检测
- 商汤自研机械臂,首款产品是AI下棋机器人:还请郭晶晶作代言
- 瑞幸「翻身」?恐言之尚早
- Open Office XML 格式里如何描述多段具有不同字体设置的段落
- Wirshark common operations and tcp three-way handshake process example analysis
- Short read or OOM loading DB. Unrecoverable error, aborting now
- I would like to ask the big guys, how to solve this error when cdc oracle initializes a 3 million table task running
- 燃炸!字节跳动成功上岸,只因刷爆LeetCode算法面试题
- 2022年五大云虚拟化趋势
- X5WebView使用
猜你喜欢

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

11 + chrome advanced debugging skills, learn to direct efficiency increases by 666%

Jenkins修改端口号, jenkins容器修改默认端口号

鸿蒙开发从hello world开始

Basic knowledge of switches

bgp dual plane experiment routing strategy to control traffic

把相亲角搬到海外,不愧是咱爸妈

跨域的五种解决方案

2022-08-09:以下go语言代码输出什么?A:否,会 panic;B:是,能正确运行;C:不清楚,看投票结果。 package main import ( “fmt“ “syn

BEVDet4D: Exploit Temporal Cues in Multi-camera 3D Object Detection 论文笔记
随机推荐
Network Saboteur
M²BEV: Multi-Camera Joint 3D Detection and Segmentation with Unified Bird’s-Eye View Representation
LeetCode·每日一题·640.求解方程·模拟构造
C# error The 'xmins' attribute is not supported in this context
Makefile missing separator. Stop.怎么解决「建议收藏」
Interface Automation Testing Basics
交换机的基础知识
Matlab画分段函数「建议收藏」
Loudi Sewage Treatment Plant Laboratory Construction Management
shell:常用小工具(sort、uniq、tr、cut)
G1和CMS的三色标记法及漏标问题
X5WebView使用
jenkins数据迁移和备份
想通这点,治好 AI 打工人的精神内耗
来看Prada大秀吗?在元宇宙里那种!
DNS欺骗-教程详解
【百度统计】用户行为分析
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama
一种能让大型数据聚类快2000倍的方法,真不戳
需要被记录的OpenStack相关的命令_自己用