当前位置:网站首页>Pointer operation - C language
Pointer operation - C language
2022-07-05 14:21:00 【Cwxh0125】
First , Let's start with a simple piece of code
#include<stdio.h>
int main(void)
{
char ac[]={0,1,2,3,4,5,6,7,8,9};
char *p = ac;
printf("p =%p\n",p);
printf("p+1 =%p\n",p+1);
int ai[]={0,1,2,3,4,5,6,7,8,9};
int *q= ai;
printf("q =%p\n",q);
printf("q+1 =%p\n",q+1);
return 0;
}After running
p =0*bffbad5a
p+1=p =0*bffbad5b
q =0*bffbad2c
q+1 =0*bffbad30
You can see p+1 Compared with p Address to add 1 and q+1 But added 4 (2c=44 30=48)
Why? ?
because sizeof(char)=1 sizeof(int)=4
So when we add one hour to a pointer Not adding one to the address But add sizeof( type ) Value

#include<stdio.h>
int main(void)
{
char ac[]={0,1,2,3,4,5,6,7,8,9};
char *p = ac;
printf("p =%p\n",p);
printf("p+1 =%p\n",p+1);
printf("*(p+1)=%d\n",*(p+1));
int ai[]={0,1,2,3,4,5,6,7,8,9};
int *q= ai;
printf("q =%p\n",q);
printf("q+1 =%p\n",q+1);
printf("*(q+1)=%d\n",*(q+1));
return 0;
}
After running *(p+1)=1 *(q+1)=1
It indicates that the array where the two pointers are located has moved an element behind Instead of adding one to the address
therefore Add... To a pointer 1 Indicates that you want the pointer to point to the next variable
int a[10];
int *p=a;
*(p+1)--->a[1]
*p++
Take out p The data referred to comes from , When you're done, drop in p Move to the next position
* High priority though , But no ++ High is often used for continuous space operations of array classes
In some CPU On , This can be translated directly into an assembly instruction
Pointer calculation :
1 Add or subtract an integer to the pointer
2 Increasing decreasing
3 Subtraction of two pointers
4 Pointer cannot multiply or divide
The two hands subtract
If the above code *p=&ac[0] *p1=&ac[5] Make p1-p obtain p1-p=5
The difference obtained is not the difference of the address It is Address difference /sizeof The result is that there are several such elements between two pointers
Pointer comparison
<,<=,==,>,>=,!= Can compare the addresses of pointers in memory
The addresses of the cells in the array must be linearly increasing
Two different types of pointers cannot be operated
Pointer type conversion
void* The pointer indicating that you don't know what to point to is calculated with char* identical ( But it's not connected ) Pointers can also convert types
int*p = &i; void*q = (void*)p;
It doesn't change p Type of variable referred to , But let later generations pass through with different eyes p Look at the variables it refers to
I don't think you are anymore int La , I think you are void!
边栏推荐
- R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to each vari
- Enjoy what you want. Zhichuang future
- 无密码身份验证如何保障用户隐私安全?
- R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)
- LeetCode_ 3 (longest substring without repeated characters)
- 基于 TiDB 场景式技术架构过程 - 理论篇
- 最简单不用证书也可以多开功能的方式
- Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
- 家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
- Google eventbus usage details
猜你喜欢

软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】

乌卡时代下,企业供应链管理体系的应对策略

openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)

Principle and performance analysis of lepton lossless compression

Share 20 strange JS expressions and see how many correct answers you can get

LeetCode_ 2 (add two numbers)

Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder

Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection

分享 12 个最常用的正则表达式,能解决你大部分问题

Introduction, installation, introduction and detailed introduction to postman!
随机推荐
魅族新任董事长沈子瑜:创始人黄章先生将作为魅族科技产品战略顾问
mysql 自定义函数 身份证号转年龄(支持15/18位身份证)
R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置
LeetCode_ 69 (square root of x)
Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
Thymeleaf 模板的创建与使用
乌卡时代下,企业供应链管理体系的应对策略
What category does the Internet of things application technology major belong to
Webrtc learning (II)
Shen Ziyu, nouveau Président de Meizu: M. Huang Zhang, fondateur de Meizu, agira comme conseiller stratégique pour les produits scientifiques et technologiques de Meizu
微服务项目部署后,无法访问静态资源,无法访问到上传到upload中的文件,解决办法
TiFlash 面向编译器的自动向量化加速
R Language ggplot2 Visualization: visualize linegraph, using Legend in Theme function. Paramètre de position emplacement de la légende personnalisée
Current situation, trend and view of neural network Internet of things in the future
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
用“新”字来吸引好奇的人群