当前位置:网站首页>指针(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;
}
边栏推荐
- Drive IT Modernization with Low Code
- 22!Beijing Changping District notified catering service enterprises with food safety problems
- BEVDet4D: Exploit Temporal Cues in Multi-camera 3D Object Detection Paper Notes
- 【jstack、jps命令使用】排查死锁
- 大佬们有遇到过这个问题吗? MySQL 2.2 和 2.3-SNAPSHOT 都这样,貌似是
- Inventory of Loudi Agricultural Products Inspection Laboratory Construction Guidelines
- MYSQL误删数据恢复
- Have you guys encountered this problem?MySQL 2.2 and 2.3-SNAPSHOT are like this, it seems to be
- 【学习笔记】Redis的持久化
- 【mysql索引实现原理】
猜你喜欢

How to describe multiple paragraphs with different font settings in Open Office XML format

代码随想录笔记_动态规划_70爬楼梯

Shell:数组

LeetCode·每日一题·640.求解方程·模拟构造

【量化交易行情不够快?】一文搞定通过Win10 wsl2 +Ubuntu+redis+pickle实现股票行情极速读写

Interface Automation Testing Basics

Comparison version number of middle questions in LeetCode

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

Real-time data warehouse practice of Baidu user product flow and batch integration

shell:正则表达式及三剑客grep命令
随机推荐
生成树协议STP(Spanning Tree Protocol)
shell:正则表达式及三剑客grep命令
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
C#中导入其它自定义的命名空间
Detailed explanation of es6-promise object
没有接班人,格力只剩“明珠精选”
NodeJs原理 - Stream(二)
【ECCV 2022|Millions of Prizes】PSG Competition: Pursuing the "Most Comprehensive" Scene Understanding
网络安全——XSS之被我们忽视的Cookie
Open Office XML 格式里如何描述多段具有不同字体设置的段落
【ECCV 2022|百万奖金】PSG大赛:追求“最全面”的场景理解
山水的高度
【黑马早报】雷军称低谷期曾想转行开酒吧;拜登正式签署芯片法案;软银二季度巨亏230亿美元;北京市消协约谈每日优鲜...
Matlab画分段函数「建议收藏」
把相亲角搬到海外,不愧是咱爸妈
X5WebView使用
【百度统计】用户行为分析
海外邮件发送指南(二)
Loudi Center for Disease Control and Prevention Laboratory Design Concept Description
一个 CRM One Order Application log 的单元测试报表