当前位置:网站首页>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.
边栏推荐
猜你喜欢

百战RHCE(第四十六战:运维工程师必会技-Ansible学习1-基础知识讲解)

在全志V853开发板试编译QT测试

Spend 2 hours a day to make up for Tencent T8, play 688 pages of SSM framework and Redis, and successfully land on Meituan

1对1视频源码——快速实现短视频功能提升竞争力

HCIA动态主机配置协议实验(dhcp)

How to use postman

百战RHCE(第四十七战:运维工程师必会技-Ansible学习2-Ansible安装配置练习环境)

大厂外包,值得拥有吗?

使用scrapy 把爬到的数据保存到mysql 防止重复

nacos项目搭建
随机推荐
“蔚来杯“2022牛客暑期多校训练营4
[Must read] Mylander valuation analysis, electrical stimulation products for pelvic and postpartum rehabilitation
Hikari连接池源码解读
每天花2小时恶补腾讯T8纯手打688页SSM框架和Redis,成功上岸美团
PyCharm usage tutorial (detailed version - graphic and text combination)
剑指offer专项突击版第17天
day_05模块
Talk about the understanding of Volatile
理解JS的三座大山
打印lua内部结构的函数调用
中国发布丨滴滴因违反网络安全法等被罚80.26亿元!调查细节公布
Jenkins--基础--6.1--Pipeline--介绍
The use of thread pool and analysis of ThreadPoolExecutor source code
让电商运营10倍提效的自动化工具,你get了吗?
新起点丨MeterSphere开源持续测试平台v2.0发布
OneinStack多版本PHP共存
The god-level Alibaba "high concurrency" tutorial "basic + actual combat + source code + interview + architecture"
不用Swagger,那我用啥?
百数应用中心——选择一款适合企业的标准应用
nacos项目搭建