当前位置:网站首页>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!
边栏推荐
- C - Divisors of the Divisors of An Integer Gym - 102040C
- 用“新”字来吸引好奇的人群
- What is the future development trend of neural network Internet of things
- 软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
- tidb-dm报警DM_sync_process_exists_with_error排查
- 直播预告|如何借助自动化工具落地DevOps(文末福利)
- 分享 12 个最常用的正则表达式,能解决你大部分问题
- Show strength. In this way, the mobile phone will not be difficult to move forward
- 04_ Use of solrj7.3 of solr7.3
- 01 、Solr7.3.1 在Win10平台下使用jetty的部署及配置
猜你喜欢

World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection

Thymeleaf 模板的创建与使用

tidb-dm报警DM_sync_process_exists_with_error排查

Current situation, trend and view of neural network Internet of things in the future

What is the future development trend of neural network Internet of things

What are the advantages and characteristics of SAS interface

How to protect user privacy without password authentication?

日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”

Introduction, installation, introduction and detailed introduction to postman!

如何将 DevSecOps 引入企业?
随机推荐
最长公共子序列 - 动态规划
Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
判断变量是否为数组
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
Matrix chain multiplication dynamic programming example
Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim
LeetCode_ 2 (add two numbers)
矩阵链乘 - 动态规划实例
Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products
Show strength. In this way, the mobile phone will not be difficult to move forward
详解Vue适时清理keepalive缓存方案
LeetCode_ 3 (longest substring without repeated characters)
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
04_ Use of solrj7.3 of solr7.3
How to protect user privacy without password authentication?
[learning notes] stage test 1
动态规划
基于 TiDB 场景式技术架构过程 - 理论篇
【学习笔记】阶段测试1