当前位置:网站首页>Array advanced improvement
Array advanced improvement
2022-07-02 22:52:00 【Soy sauce;】
1. One dimensional array
1.1 Concept :
- Element type angle : An array is an ordered collection of variables of the same type
- Memory angle : A contiguous large area of memory space

Before we discuss multidimensional arrays , We also need to learn a lot about one-dimensional arrays . First, let's learn a concept .
1.2 One dimensional array array name :
Consider the following statements :
int a; int b[10]; |
We put a It's called scalar , Because it's a single value , The type of this variable is an integer . We put b It's called an array , Because it's a collection of values . Subscripts are used with names , Used to identify a specific value in the collection . for example ,b[0] Represents an array b Of the 1 It's worth ,b[4] It means the first one 5 It's worth . Each value is a specific scalar .
So the question is b What is the type of ? What does it mean ? A logical answer is that it represents the entire array , But that's not the case . stay C in , In almost all expressions for array names , The value of the array name is a constant pointer , That is, the address of the first element of the array . Its type depends on the type of array element : If they are int type , So the type of array name is “ Point to int Constant pointer to ”; If they are other types , Then the type of array name is “ Point to Other types Constant pointer to ”.
Excuse me, : Is the array first address pointer equivalent to the array name ? |
The answer is no Of . When array names are used in expressions , The compiler will produce a pointer constant . Under what circumstances can an array not be used as a pointer constant ? In the following Two scenarios Next : Except for these two kinds , The rest are equivalent
|

int arr[10]; //arr = NULL; //arr As a pointer constant , Do not modify the int *p = arr; // here arr Used as a pointer constant printf("sizeof(arr):%d\n", sizeof(arr)); // here sizeof The result is the length of the entire array printf("&arr type is %s\n", typeid(&arr).name()); //int(*)[10] instead of int* |
1.3 Subscript reference
int arr[] = { 1, 2, 3, 4, 5, 6 }; |
*(arr + 3) , What does this expression mean ?
First , We say that an array is a pointer to an integer in an expression , So this expression represents arr The pointer moved back 3 The length of an element . Then get the value of the location from the new address through the indirect access operator . The execution process of this and subscript reference is exactly the same . So the following expression is equivalent , And the subscript can be negative .
*(arr + 3) arr[3] |
problem 1: Whether the array subscript can be negative ?
Tolerable , Equivalent to dereferencing

problem 2: Please read the following code , Say the result :
int arr[] = { 5, 3, 6, 8, 2, 9 }; int *p = arr + 2; printf("*p = %d\n", *p); Show the machine printf("*p = %d\n", p[-1]); Show people |
So whether to use subscripts or pointers to manipulate arrays ? For most people , Subscripts will be more readable .
1.4 Array and pointer Application
Pointers and arrays are not equal . To illustrate the concept , Consider the following two statements :
int a[10]; int *b; |
When declaring an array , The compiler allocates memory space for the array according to the number of elements specified in the declaration , And then create the array name , Point to the beginning of this space . When declaring a pointer variable , The compiler only allocates memory space for the pointer itself , No memory space is allocated for any integer value , The pointer is not initialized to point to any existing memory space .
therefore , expression *a It's perfectly legal , But the expression *b It's illegal .*b Will access an indeterminate location in memory , Will cause the program to terminate . On the other hand b++ It can be compiled ,a++ But not , because a Is a constant value .
1.5 Array Array name as function parameter
What happens when an array name is passed to a function as an argument ? We now know that the array name is actually a pointer to the first... Of the array 1 Pointers to elements , So it is clear that what is passed to the function at this time is a copy of the pointer . So the formal parameter of a function is actually a pointer . But in order to make it easier for novice programmers , The compiler also accepts function parameters in the form of arrays . Therefore, the following two function prototypes are equal :
int print_array(int *arr); int print_array(int arr[]); |
We can use any kind of declaration , But which one is more accurate ? The answer is the pointer . Because the argument is actually a pointer , Not an array . Again sizeof arr The value is the length of the pointer , Not the length of the array .
Now we know , Why is it unnecessary to specify the number of elements in a one-dimensional array , Because a formal parameter is just a pointer , There is no need to allocate memory for array parameters . On the other hand , In this way, the function cannot know the length of the array . If the function needs to know the length of the array , It must explicitly pass a length parameter to the function .
2. Multidimensional arrays
2.1 Concept :
If the dimension of an array is more than 1 individual , It is called a multidimensional array . The next example is a two-dimensional array .
void test01(){ // 2D array initialization int arr1[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int arr2[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int arr3[][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Print 2D array for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j ++){ printf("%d ",arr1[i][j]); } printf("\n"); } } |
2.2 Array name of two-dimensional array
The value of a dimension group name is a pointer constant , Its type is “ Pointer to element type ”, It points to the... Of the array 1 Elements . The same goes for multidimensional arrays , The array name of the multidimensional array also points to the first element , Only the first element is an array . for example :
int arr[3][10] |
It can be understood that this is a one-dimensional array , Contains 3 Elements , But each element just contains 10 Array of elements .arr It means the second... Pointing to it 1 Pointers to elements , therefore arr Is a point that contains 10 Pointer to an array of integer elements .
3. Pointer to array ( Array pointer )
3.1 Concept :
(&arr Not the first element address , Represents the entire array , It's an array pointer )
Array pointer , It's a pointer , Pointer to array . Array pointer , refer to Array Named The pointer , That is, the first element of the array Address The pointer to . That is, the pointer to the array . example :int (*p)[10]; p Is a pointer to the array , Also known as array pointer .
The type of the array is determined by Element type and Array size Joint decision :int array[5] The type of int[5];C Language is available through typedef Define an array type :
3.2 There are three ways to define array pointers :
// Mode one void test01(){ // Define the array type first , Then define the array pointer with the array type int arr[10] = { 1,2,3,4,5,6,7,8,9,10}; // Yes typedef Is to define the type , If not, define variables , The following code defines an array type ArrayType typedef int(ArrayType)[10]; //int ArrayType[10]; // Define an array , Array name ArrayType ArrayType myarr; // Equivalent to int myarr[10]; ArrayType* pArr = &arr; // Defines an array pointer pArr, And the pointer points to the array arr for (int i = 0; i < 10;i++){ printf("%d ",(*pArr)[i]); } printf("\n"); } // Mode two void test02(){ int arr[10]; // Defines the array pointer type typedef int(*ArrayType)[10]; ArrayType pArr = &arr; // Defines an array pointer pArr, And the pointer points to the array arr for (int i = 0; i < 10; i++){ (*pArr)[i] = i + 1; } for (int i = 0; i < 10; i++){ printf("%d ", (*pArr)[i]); } printf("\n"); } // Mode three ( Directly use array pointer variables )( Commonly used ) void test03(){ int arr[10]; int(*pArr)[10] = &arr; for (int i = 0; i < 10; i++){ (*pArr)[i] = i + 1; } for (int i = 0; i < 10; i++){ printf("%d ", (*pArr)[i]); } printf("\n"); } |



3.3 Application scenarios :
A little bit of development experience C Language programmers should have defined the following functions :
void fun(char *p, int len);
Parameters len Used to describe pointers p Memory length of index , Such a definition obviously cannot guarantee the transmission to fun() The memory length of the function is equal to the expected value . If fun() The developer of the function expects to pass to fun() Pointer to function p The memory length of the index is 10, Then we can only hope that his colleagues will strictly abide by the developer's intentions ( It's not easy ), After all
fun(p, 9);
fun(p, 110);
It's all legal , Such a function call compiler will not produce any errors or warnings . If fun() Functions are defined in the form of array pointers , It's different , relevant C The language code is as follows :
void fun(char (*p)[10]);
This way of definition forces fun() The caller of the function passes a pointer to the memory with the specified length of the index , Otherwise, the compiler will issue a warning or error message .
边栏推荐
- 佩服,竟然有人把高等数学这么晦涩难懂的科目,讲解得如此通俗易懂
- 服务器响应状态码
- Source code analysis - lightweight asynchronous crawler framework Ruia
- 【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?
- [chestnut sugar GIS] ArcMap - how to batch modify the font, color, size, etc. of annotation elements
- 【板栗糖GIS】global mapper 如何通过dsm批量制作贴地等高线
- uniapp微信登录返显用户名和头像
- 开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...
- [Luogu p1541] tortoise chess [DP]
- SimpleITK使用——4. 奇怪的問題
猜你喜欢

wait解决僵尸进程

开发者分享 | HLS, 巧用AXI_master总线接口指令的定制并提升数据带宽-面积换速度...

Wait to solve the zombie process

Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed

Socket socket c/s end process

SimpleITK使用——4. 奇怪的問題

Oracle PL / SQL programming

Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
![[foreign journal] sleep and weight loss](/img/81/42dcfae19e72a0bc761cb7a40fe5d5.jpg)
[foreign journal] sleep and weight loss

建立自己的网站(22)
随机推荐
PMP项目整合管理
Go condition variable
加油站[问题分析->问题转换->贪心]
Regular expression (2)
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
Rails 3 activerecord: sort by association count - rails 3 activerecord: order by count on Association
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
Jerry's modification does not require long press the boot function [chapter]
数学建模——图与网络模型及方法(一)
牛客网:龙与地下城游戏
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖
服务器响应状态码
How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
Wait to solve the zombie process
数据库系统概论第一章简答题-期末考得怎么样?
uniapp微信登录返显用户名和头像
I admire that someone explained such an obscure subject as advanced mathematics so easily
数组进阶提高
Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly