当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
初学者怎么快速学会SQL
AutoJs学习-AES加解密
js引擎运行中的预解析(变量提升和函数提升)及相关实操案例
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
system_error错误处理库学习
Rust 从入门到精通03-helloworld
剑指offer专项突击版第17天
李航《统计学习方法》笔记之朴素贝叶斯法
三国演义小说
UVM事务级建模
YugaByte adds Voyager migration service in its 2.15 database update
百数应用中心——选择一款适合企业的标准应用
leetcode:81. 搜索旋转排序数组 II
AutoJs学习-实现谢尔宾斯基三角
每天花2小时恶补腾讯T8纯手打688页SSM框架和Redis,成功上岸美团
了解下C# 不安全代码
Tencent T8 architect, teach you to learn small and medium R&D team architecture practice PDF, senior architect shortcut
Qt读取文件中内容(通过判断GBK UTF-8格式进行读取显示)
ORBSLAM代码阅读









