当前位置:网站首页>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;
}
边栏推荐
- bgp dual plane experiment routing strategy to control traffic
- Real-time data warehouse practice of Baidu user product flow and batch integration
- Fragment's show and hide
- 2022-08-09: What does the following go code output?A: No, it will panic; B: Yes, it can run correctly; C: Not sure, see the voting result.package main import (“fmt“ “syn
- 【iOS】Organization of interviews
- YTU 2295: KMP pattern match one (string)
- The recursive recursive Fighting_ silver study ah but level 4
- Have you guys encountered this problem?MySQL 2.2 and 2.3-SNAPSHOT are like this, it seems to be
- 交换机的基础知识
- Import other custom namespaces in C#
猜你喜欢
2022-08-09: What does the following go code output?A: No, it will panic; B: Yes, it can run correctly; C: Not sure, see the voting result.package main import (“fmt“ “syn
接口自动化测试基础篇
矩阵键盘&基于51(UcosII)计算器小项目
ABAP 里文件操作涉及到中文字符集的问题和解决方案试读版
MYSQL误删数据恢复
Jiugongge lottery animation
浙大、阿里提出DictBERT,字典描述知识增强的预训练语言模型
Detailed explanation of es6-promise object
3DS MAX batch export file script MAXScript with interface
Short read or OOM loading DB. Unrecoverable error, aborting now
随机推荐
AWS 安全基础知识
Jenkins修改端口号, jenkins容器修改默认端口号
Interface Automation Testing Basics
Fragment-hide和show
Stream通过findFirst()查找满足条件的一条数据
瑞幸「翻身」?恐言之尚早
Short read or OOM loading DB. Unrecoverable error, aborting now
3DS MAX 批量导出文件脚本 MAXScript 带界面
G1和CMS的三色标记法及漏标问题
M²BEV: Multi-Camera Joint 3D Detection and Segmentation with Unified Bird’s-Eye View Representation
BEVDet4D: Exploit Temporal Cues in Multi-camera 3D Object Detection 论文笔记
mSystems | Zhongnong Wang Jie Group Reveals the Mechanisms Affecting Soil "Plastic Interstitial" Microbial Communities
Makefile missing separator. Stop.怎么解决「建议收藏」
Open Office XML 格式里如何描述多段具有不同字体设置的段落
系统的安全和应用(不会点安全的东西你怎么睡得着?)
商汤自研机械臂,首款产品是AI下棋机器人:还请郭晶晶作代言
【目标检测】小脚本:提取训练集图片与标签并更新索引
LeetCode·297.二叉树的序列化与反序列化·DFS·BFS
The recursive recursive Fighting_ silver study ah but level 4
锂电池技术