当前位置:网站首页>Pointer (preliminary solution of C language)
Pointer (preliminary solution of C language)
2022-08-10 13:33:00 【quack quack】
In computer science, a pointer is an object in a programming language. Using an address, its value directly points to a value stored in another place in the computer's memory. The desired variable unit can be found through the address. It can be said that the address points to theThe variable unit is therefore called an "address" of the image.It means that the memory cell with its address can be found.
#include
int main() {
printf("%d\n",sizeof(char*));
printf("%d\n", sizeof(int*));
printf("%d\n", sizeof(double*));
printf("%d\n", sizeof(short*));
return 0;
}
*Prove that the pointer occupies four bytes
*value character & address character
- The type of the pointer determines the size of the space that can be accessed when the pointer is dereferenced, and determines what type the pointer variable points to
int 4 bytes
char 1 byte
double 8 bytes
#define _CRT_SECURE_NO_WARNINGS
#includeint 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;
}
- Wild pointer (where the pointer points to is unknown)
Cause: 1, pointer not initialized
2, pointer out-of-bounds access
int a[]={0};
int *p=a;
int i=0;
for(i=0;i<11,1++){
p++;
}
3, the address in the calling function is returned to the system
#define _CRT_SECURE_NO_WARNINGS
#include
int *test() {
int a = 10;
return &a;
}
int main() {
int* p = test();//After the function is called, the allocated address of 10 will be automatically cancelled, and *p will become a wild pointer
*p = 20;
Return 0;
}
*p=NULL pointer cannot be accessed when it is empty
- Pointer Arithmetic
1, pointer + integer
2, pointer-integer
3, the relational operation of pointers
Pointer+-Integer:
#define _CRT_SECURE_NO_WARNINGS
#include
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
#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;
}
The big address-small address is the number of intermediate elements + 1, and the result is unpredictable when two different array addresses are subtracted
#define _CRT_SECURE_NO_WARNINGS
#include
#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;
}
边栏推荐
- 一种能让大型数据聚类快2000倍的方法,真不戳
- 一种能让大型数据聚类快2000倍的方法,真不戳
- 日志@Slf4j介绍使用及配置等级
- 高数_证明_弧微分公式
- 开源SPL消灭数以万计的数据库中间表
- C# 当前上下文中不存在InitializeComponent()
- Guidelines for Sending Overseas Mail (2)
- 高数_证明_曲率公式
- Stream通过findFirst()查找满足条件的一条数据
- R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的gt_highlight_rows函数高亮(highlight)表格中特定的数据行、配置高亮行的特定数据列数据加粗
猜你喜欢
kubernetes介绍
Basic knowledge of switches
Redis 定长队列的探索和实践
Detailed explanation of es6-promise object
jenkins数据迁移和备份
BEVDet4D: Exploit Temporal Cues in Multi-camera 3D Object Detection Paper Notes
浙大、阿里提出DictBERT,字典描述知识增强的预训练语言模型
Guidelines for Sending Overseas Mail (2)
Ethernet channel 以太信道
Prada, big show?In the yuan in the universe that!
随机推荐
AWS 安全基础知识
表中存在多个索引问题? - 聚集索引,回表,覆盖索引
一种能让大型数据聚类快2000倍的方法,真不戳
【jstack、jps命令使用】排查死锁
【量化交易行情不够快?】一文搞定通过Win10 wsl2 +Ubuntu+redis+pickle实现股票行情极速读写
SenseTime self-developed robotic arm, the first product is an AI chess-playing robot: Guo Jingjing is also invited as an endorsement
LeetCode·每日一题·640.求解方程·模拟构造
生成树协议STP(Spanning Tree Protocol)
神了!阿里数据库专家纯手写了这份604页的Oracle+MySQL攻坚指南
作业8.9 构建TCP协议的服务器
矩阵键盘&基于51(UcosII)计算器小项目
需要被记录的OpenStack相关的命令_自己用
汉字检测和关键词检测
Stream通过findFirst()查找满足条件的一条数据
开源SPL消灭数以万计的数据库中间表
Comparison version number of middle questions in LeetCode
Nanodlp v2.2/v3.0 light curing circuit board, connection method of mechanical switch/photoelectric switch/proximity switch and system state level setting
SecureCRTPortable – 破解
kubernetes介绍
广东10个项目入选工信部2021年物联网示范项目名单