当前位置:网站首页>Advanced pointer depth "six" (two-dimensional array related knowledge)
Advanced pointer depth "six" (two-dimensional array related knowledge)
2022-07-22 08:48:00 【Populus euphratica under the desert】
Personal home page : Welcome to ——> Populus euphratica under the desert
This issue focuses on : Two dimensional array related understanding chain
I hope you will Study and work in a happy mood .
Catalog
Memory layout of two-dimensional array
Three dimensional array memory
Two dimensional array
Memory layout of two-dimensional array
First of all, in our daily study , The schematic diagram of two-dimensional array is mostly in the form of matrix , Well, that's right ?
Take an example to analyze :
char a[4][5] = { 0 }; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { printf("%p\n", &a[i][j]); } } return 0;
Watch carefully , It is found that the memory in the two-dimensional array is continuous , This is normal , After all, when we talk about arrays, we say that arrays should open up space as a whole , So it will continue , Well, it seems different from the usual drawing , It should look like the following .
Only in this way can it meet the memory layout of such a two-dimensional array , In this way, we can think of a two-dimensional array as 4 It consists of a one-dimensional array , Each array is int [5] type .
Three dimensional array memory
What about multidimensional arrays ?( Three dimensional array )
We can think of three-dimensional arrays as 4 One by one int[4][5] A one-dimensional array of , and int[4][5] It can also be seen as 4 It consists of a one-dimensional array , Each array is int [5] type . This is a bit like a successive downgrade , therefore No matter how many dimensional arrays are, they can be regarded as composed of some one-dimensional arrays .
Two dimensional array name
There is also some use of two-dimensional array names, which is a little confusing , Let's have a deep understanding of array names .
Look at a code :
char c[3][4] = { 0 }; printf("%p\n", &c); printf("%p\n", c); printf("%p\n", &c[0][0]);
Here we can see that the expressions of different array names are the same value , Now let's draw and analyze :
&c Is the overall address of the array ,c Is the address of the first element of the array ( Consider a two-dimensional array as the first element type after a one-dimensional array ),&c[0][0] Is the first... In the array char Type of address , Because for any target that can get an address , The address it takes out is the lowest address , Just the lowest address is &c[0][0] The address of , So in numerical terms, the number of addresses is the same , But the step size is the same , The root cause is because of different types .
Two dimensional array example
Let's take a classic example , To further understand two-dimensional arrays , Pointer array
int a[5][5] = { 0 }; int(*p)[4] = a; printf("%p\n%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);
Now draw a picture to explain :
First of all, the starting address is the same ,p It's an array pointer , The type of array is int [4], Every time p+1 Is to move back four shaping size spaces , That is, the color part on the figure , And two-dimensional arrays int [5][5] Can be seen as 5 One dimensional array , The type is int [5], Every time +1 Is backward access 5 The size of a plastic .
therefore p[4][2] It's light brown ,&p[4][2] It's the place shown in the picture ,a[4][2] It's dark brown ,&a[4][2] As shown in the figure , Subtract the address value , Represents the number of types of the first element in an array , then %p No match -4,%d It's symbolic -4, That's easy .
Dereference related
About two-dimensional array dereference related .
int a = 10; int *p = &a; int **pp = &p; p = 100; *p = 100; pp = 100; *pp = 100; **pp = 100;
This is the general direction diagram , First p Yes a The lowest address ,pp Yes p The lowest address .p discharge a Get address
pp discharge p The address of .
First p = 100 Refer to p The direction of has changed .
*p = 100, because *p yes a Value , Is to put a Change the value in to 100.
pp = 100, Is actually the pp The direction of has been changed .
*pp = 100, It's actually *pp Namely p The content of , Namely &a, So the p The direction of has changed , and p = 100 The effect is the same .
**pp = 100 In fact, it is to dereference and access p The content of , In dereference , Become right p Quoting , visit a Value , hold a The value of the into 100, Actually sum *p = 100 The effect is the same .
Next up
In the next issue, we'll talk about function pointers
The next issue is more exciting ~!~!
边栏推荐
- How to open an account for agricultural futures? Who can open Everbright futures?
- Verilog -- Serial four bit adder and leading four bit adder 74hc283
- 指针的深度解刨《五》
- v-7
- 张驰咨询:六西格玛如何帮助公司减少客户投诉
- Quantitative transaction Diary - summary in February 2021
- JS时间和时间戳的转换
- 多线程与线程池
- 电商大促就靠RPA,摆脱重复劳动,急速提效
- Verilog -- 74hc85 four bit numeric comparator and extended to 16 bit numeric comparator
猜你喜欢
What is RPA? Recommend automated tools that allow e-commerce operators to operate 10 times more efficiently
Self study golang [3.7map exercise code]
[C语言] 文件操作《一》
多线程与线程池
软考中级【数据库系统工程师】第0章:如何自学备考,考试介绍考什么,备考教材,上午和下午的体型分数分布,备考课程链接,个人备考感谢
Self study the definition of golang [3.5go language array, range keyword] array, and use the for loop to traverse one-dimensional array
Quantify three types of market in trading
Pytoch learning (I) Deep learning review and introduction to pytoch
量化交易日记-回撤分析-2021年02月6日
指针深度解刨《四》(指针和数组的 “亲密“ 关系)
随机推荐
指针总结篇
SPI debugging is not successful, it is likely that you connected the wrong cable!!
量化交易日记-2021年01月总结
Fastjason, Jackson handling time and date issues precautions
vscode添加自定义注释
张驰咨询:某包装印刷集团第五期六西格玛项目通过专家评审
RunTime+compiler和RunTime only版本的区别
【flex布局快速上手】快速理解flex布局用法,通过常见的四个布局案例解释【详细注释,一看就会】
Sharing the ultimate breakthrough strategy used by professional traders for many years (with a full set of trading templates)
Quantify three types of market in trading
指针的深度解刨《七》(函数指针相关知识)
Redis的持久化方式RDB和AOF的区别
解释内存中的栈(stack)、堆(heap)和方法区(method area)的用法
Self study golang [Chapter 3: the first go language program] use GoLand to create the first go program, the main function and init function, and use go to run Windows commands and realize the data out
Verilog -- Serial four bit adder and leading four bit adder 74hc283
指针的深度解刨《五》
动态内存管理
How to put the horizontal flashing cursor (_) in notepad++ Change to vertical flashing cursor style (|)?
ASP. Net core deployment Manual: 2 Hyper-V virtual machine
How to delete colored lines in Excel