当前位置:网站首页>指针(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;
}
边栏推荐
- 燃炸!字节跳动成功上岸,只因刷爆LeetCode算法面试题
- 网络安全——XSS之被我们忽视的Cookie
- NodeJs原理 - Stream(二)
- Interface Automation Testing Basics
- Inventory of Loudi Agricultural Products Inspection Laboratory Construction Guidelines
- Solution for "Certificate not valid for requested usage" after Digicert EV certificate signing
- 【iOS】面试整理
- 【ECCV 2022|百万奖金】PSG大赛:追求“最全面”的场景理解
- BEVDet4D: Exploit Temporal Cues in Multi-camera 3D Object Detection Paper Notes
- I would like to ask the big guys, how to solve this error when cdc oracle initializes a 3 million table task running
猜你喜欢
【ECCV 2022|百万奖金】PSG大赛:追求“最全面”的场景理解
Stream通过findFirst()查找满足条件的一条数据
浙大、阿里提出DictBERT,字典描述知识增强的预训练语言模型
Short read or OOM loading DB. Unrecoverable error, aborting now
Basic knowledge of switches
Guidelines for Sending Overseas Mail (2)
2022年五大云虚拟化趋势
【百度统计】用户行为分析
Wirshark common operations and tcp three-way handshake process example analysis
矩阵键盘&基于51(UcosII)计算器小项目
随机推荐
2022年五大云虚拟化趋势
【jstack、jps命令使用】排查死锁
Solution for "Certificate not valid for requested usage" after Digicert EV certificate signing
Loudi Sewage Treatment Plant Laboratory Construction Management
11 + chrome advanced debugging skills, learn to direct efficiency increases by 666%
shell:正则表达式及三剑客grep命令
Reversing words in a string in LeetCode
Basic knowledge of switches
Codeforces Round #276 (Div. 1) D. Kindergarten
跨域的五种解决方案
LeetCode中等题之搜索二维矩阵
Requirements for the construction of Loudi stem cell preparation laboratory
A detailed explanation of implementation api embed
C#报错 The ‘xmins‘ attribute is not supported in this context
mSystems | Zhongnong Wang Jie Group Reveals the Mechanisms Affecting Soil "Plastic Interstitial" Microbial Communities
汉字检测和关键词检测
Have you guys encountered this problem?MySQL 2.2 and 2.3-SNAPSHOT are like this, it seems to be
SenseTime self-developed robotic arm, the first product is an AI chess-playing robot: Guo Jingjing is also invited as an endorsement
Short read or OOM loading DB. Unrecoverable error, aborting now
【百度统计】用户行为分析