当前位置:网站首页>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.
边栏推荐
- It's time for bank data people who are driven crazy by reporting requirements to give up using Excel for reporting
- Jenkins--基础--5.4--系统配置--全局工具配置
- 主流监控系统工具选型及落地场景参考
- 每天花2小时恶补腾讯T8纯手打688页SSM框架和Redis,成功上岸美团
- JS中的数组方法
- day_05模块
- Qt读取文件中内容(通过判断GBK UTF-8格式进行读取显示)
- 四字节的float比八字结的long范围大???
- 在 QT Creator 上配置 opencv 环境的一些认识和注意点
- 【技术分享】OSPFv3基本原理
猜你喜欢
随机推荐
mysql连接池的实现
MySQL读写分离与主从延迟
HCIA动态主机配置协议实验(dhcp)
Pycharm (1) the basic use of tutorial
Overview of Edge Computing Open Source Projects
稳定币:对冲基金做空 Tether 的结局会是什么?
第15章 泛型
Rust from entry to master 03-helloworld
The use of thread pool and analysis of ThreadPoolExecutor source code
Jenkins--基础--5.4--系统配置--全局工具配置
The packet capture tool Charles modifies the Response step
Fiddler(七) - Composer(组合器)克隆或者修改请求
mysql进阶(二十一)删除表数据与数据库四大特性
What is the function of page directive contentPage/pageEncoding in JSP page?
RetinaFace: Single-stage Dense Face Localisation in the Wild
百数应用中心——选择一款适合企业的标准应用
AutoJs学习-存款计算器
瑞吉外卖项目剩余功能补充
数据库mysql
[Concurrent programming] - Thread pool uses DiscardOldestPolicy strategy, DiscardPolicy strategy









