当前位置:网站首页>Two-dimensional array piecemeal knowledge sorting
Two-dimensional array piecemeal knowledge sorting
2022-08-02 09:22:00 【meow on keyboard】
Table of Contents
Structures of one- and two-dimensional arrays
2. Arrays as function parameters
1. One-dimensional array parameter transfer
2. Two-dimensional array parameter transfer
Three, use pointer to control two-dimensional array
Foreword
Hello, everyone.The structure of two-dimensional arrays in C language is more complicated, especially after combining with pointers.Since I have been learning C++ recently, I feel unfamiliar with some knowledge of two-dimensional arrays, so I wrote a blog to summarize this knowledge.
Structure of one- and two-dimensional arrays
We can understand a two-dimensional array as a one-dimensional array first, and then the element type of the one-dimensional array is also a one-dimensional array, that is, a nested array in an array.
Understand each row as an independent array, arr[i] selects the array in row i, and arr[i][j] selects the jth element in row i.
How is the two-dimensional array distributed in memory?
int main(){int arr[3][3] = { {1,2,3},{4,5,6},{7,8,9} };for (int i = 0; i < 3; i++){for (int j = 0; j< 3; j++){printf("%p\n", &arr[i][j]);}}}
After printing, it is found that the two-dimensional array is continuously distributed in the memory, but for the convenience of understanding, we imagine it as two-dimensional.
2. Arrays as function parameters
1. One-dimensional array parameter transfer
Let's take a look at one-dimensional array parameters first.
int size(int arr[]) //int size(int* arr){return sizeof(arr);}int main(){int arr[3];int size1=sizeof(arr);int size2 = size(arr);printf("%d %d", size1,size2);}
It can be seen from the results that when an array is used as a function parameter, the function will not stupidly copy the entire array, but will take the address of the first element of the array.Therefore, the parameters of the one-dimensional array parameter transfer function can be written as either an array or a pointer. Anyway, the address of the first element is passed in the past.
2. Two-dimensional array parameter transfer
In the one-dimensional array arr, arr is equivalent to the address of the first element, which is equivalent to &arr[0].And &arr is equivalent to the address of the entire array, dereferencing the entire array is equal to the address of the first element (to be honest, I don't know why, but the syntax is so prescribed), if you want to accept the address of the entire element, you must use the array pointer.
In the two-dimensional array arr, analogous to a one-dimensional array, arr is the address of the first element, and the first element of the two-dimensional array is the first row of the array, so arr is the address of the entire first row of the array.Therefore, the two-dimensional array parameter is received by a two-dimensional array or by an array pointer.
void test1(int arr[][3])//It is the same as when the two-dimensional array is defined, the number of row elements can be omitted, and the column cannot be{}void test2(int(*p)[3]){}int main(){int arr[3][3];test1(arr);test2(arr);}
Third, use a pointer to control a two-dimensional array
For the two-dimensional array arr, arr is the address of the first row of the array, arr+i is the address of the i-th row array, and *(arr+i) is the address of the first element of the i-th row array (mentioned above, the address dereference of an array is equal to the address of the first element).So *(*(arr+i)+j) gets the element arr[i][j].
Summary
That's all for today.This article mainly explains some confusing pieces of knowledge about two-dimensional arrays, hoping to help you.Thank you for reading, I have a long time in Japan, and I look forward to seeing you next time.
边栏推荐
猜你喜欢
干货|如何在海量文件系统中选择合适自己的文件系统
堪称神级的阿里巴巴“高并发”教程《基础+实战+源码+面试+架构》
spark:商品热门品类TOP10统计(案例)
Jenkins--基础--07--Blue Ocean
百战RHCE(第四十七战:运维工程师必会技-Ansible学习2-Ansible安装配置练习环境)
AutoJs学习-存款计算器
PyCharm usage tutorial (detailed version - graphic and text combination)
Rust from entry to master 03-helloworld
被报表需求逼疯的银行数据人,是时候放弃用Excel做报表了
PyQt5安装配置(PyCharm) 亲测可用
随机推荐
cococreator dynamically set sprite
Hikari连接池源码解读
被报表需求逼疯的银行数据人,是时候放弃用Excel做报表了
C语言volatile关键字、内嵌汇编volatile与编译器的爱恨情仇
高效时代,电商运营如何靠RPA快速提效?
破解wifi密码 暴力破解 保姆式教学
Qt读取文件中内容(通过判断GBK UTF-8格式进行读取显示)
JS中的数组方法
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
mysql连接池的实现
腾讯T8架构师,教你学中小研发团队架构实践PDF,高级架构师捷径
主流监控系统工具选型及落地场景参考
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
day_05 time 模块
Tencent T8 architect, teach you to learn small and medium R&D team architecture practice PDF, senior architect shortcut
tf中tensor的大小输出
了解下C# 多线程
动态规划每日一练(2)
测试时大量TIME_WAIT
AlterNET Studio用户界面设计功能扩展