当前位置:网站首页>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;
}
边栏推荐
- 代码随想录笔记_动态规划_70爬楼梯
- [target detection] small script: extract training set images and labels and update the index
- YTU 2295: KMP pattern match one (string)
- 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
- AtCoder初学者比赛077 D -小多
- Keithley DMM7510 accurate measurement of ultra-low power consumption equipment all kinds of operation mode power consumption
- Inventory of Loudi Agricultural Products Inspection Laboratory Construction Guidelines
- 矩阵键盘&基于51(UcosII)计算器小项目
- 教育Codeforces轮41(额定Div。2)大肠Tufurama
- Overview of Loudi Petrochemical Experiment Design and Construction Planning
猜你喜欢
![[target detection] small script: extract training set images and labels and update the index](/img/9d/0f88b484cee1b85df6bc1153d9b6b4.png)
[target detection] small script: extract training set images and labels and update the index

2022-08-09:以下go语言代码输出什么?A:否,会 panic;B:是,能正确运行;C:不清楚,看投票结果。 package main import ( “fmt“ “syn
雨水中存在的PFAS化学物质对饮用水安全构成了威胁

Ethernet channel Ethernet channel

LeetCode·297.二叉树的序列化与反序列化·DFS·BFS

Code Casual Recording Notes_Dynamic Programming_70 Climbing Stairs

Prada, big show?In the yuan in the universe that!

Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer Paper Notes

BEVDet4D: Exploit Temporal Cues in Multi-camera 3D Object Detection 论文笔记

机器学习实战(2)——端到端的机器学习项目
随机推荐
recursive recursive function
【百度统计】用户行为分析
【黑马早报】雷军称低谷期曾想转行开酒吧;拜登正式签署芯片法案;软银二季度巨亏230亿美元;北京市消协约谈每日优鲜...
Fragment-hide and show
Nanodlp v2.2/v3.0 light curing circuit board, connection method of mechanical switch/photoelectric switch/proximity switch and system state level setting
mSystems | Zhongnong Wang Jie Group Reveals the Mechanisms Affecting Soil "Plastic Interstitial" Microbial Communities
X5WebView使用
指针(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
M²BEV: Multi-Camera Joint 3D Detection and Segmentation with Unified Bird’s-Eye View Representation
Shell:数组
一个 CRM One Order Application log 的单元测试报表
【量化交易行情不够快?】一文搞定通过Win10 wsl2 +Ubuntu+redis+pickle实现股票行情极速读写
C# InitializeComponent() does not exist in the current context
系统的安全和应用(不会点安全的东西你怎么睡得着?)
CodeForces - 834C
C#中导入其它自定义的命名空间
LeetCode·297.二叉树的序列化与反序列化·DFS·BFS
CodeForces - 811A
A can make large data clustering method of 2000 times faster, don't poke