C language - pointer array and array pointer

Table of contents

1. Two-dimensional array

2. Array of pointers

(1) Concept

 (2) Writing method

(3) The pointer array simulates a two-dimensional array

3. Array pointer

(1) Concept

(2) Print a one-dimensional array using an array pointer

 (3) Simulate the parameter passing of two-dimensional arrays


1. Two-dimensional array

First of all, we need to understand some related concepts between two-dimensional arrays and pointer variables.

Two-dimensional array:

int arr [ 3 ][ 5 ]  

Like a one-dimensional array, it is stored continuously in memory, and the difference between each element is the same. For example, in the x86 environment, the difference between each adjacent element in a two-dimensional array is 4 bytes.

However, due to the particularity of the two-dimensional array, we usually print the two-dimensional array into a matrix form.

But it is different from a one-dimensional array. The first address of a one-dimensional array is the address of its first element, while the first address of a two-dimensional array is the address of the first row.

Each row of a two-dimensional array is a one-dimensional array, and this one-dimensional array can be regarded as an element of a two-dimensional array.

So a two-dimensional array can also be considered an array of one-dimensional arrays.

Then int arr[3][5] is regarded as a one-dimensional array, then the array name is arr[3].

2. Array of pointers

(1) Concept

First of all, an array of pointers is an array, and pointer variables of the same type, or addresses, are stored in the array.

Just like an integer array, an integer variable is stored in an integer array, and a character array stores a character type variable in a character array.

 There are many types of pointer variables, such as int*, char*, short*... So, pointer arrays also have many different types.

 (2) Writing method

int*parr[6]:

parr array name

int* indicates that the type of the element pointed to by the address stored in the array is int, and the type of these addresses is also int*

[6] indicates the size of the pointer array.

(3) The pointer array simulates a two-dimensional array

 Among them, arr1, arr2, and arr3 represent the array name, and the array name represents the address of the first element of the array, which is equivalent to a pointer.

int * represents the type of those pointer variables in the pointer array.

And because of the characteristics of two-dimensional arrays, parr[ i ] can be expressed as an array name.

Another way of writing the array name is *(arr+i) where arr represents the array name and i represents traversal.

And because parr[ i ] == *( parr + i )

So another way of writing parr[ i ][ j ]: *( *(parr+i)+ j ) where *(parr+i) is the array name, j is the traversal

3. Array pointer

(1) Concept

First of all, we must realize that we have learned pointer arrays before, and pointer arrays are arrays, which are arrays that store pointers.

The array pointer is a pointer to the array, which stores the address of the array.

For example:

int arr[10]; 

int(* p)[10] = &arr;

Before that, let's understand the address of the array:

Through the study of one-dimensional arrays, we know that the array name represented by arr is also represented as the address of the first element of the array, and &arr represents the address of the entire array.

Then, following our knowledge of pointers, we can represent arr with int * and &arr with int(*) [10].

As shown above, the address of the entire array is stored in the pointer variable p.

(2) Print a one-dimensional array using an array pointer

 

 (*p)[ i ] Among them, (*p) can be expressed as an array name, the reason is &arr = int (*p)[10] The address of the entire array is given to the pointer variable p, which is equivalent to p being &arr for conversion ( *p) = (*&arr) where * and & cancel each other out, so (*p) is equivalent to the array name.

Not enough The above writing method is not recommended.

The writing in the picture below is more suitable for the public.

 (3) Simulate the parameter passing of two-dimensional arrays

Assumptions:

int arr[3][5] = {
   
   {1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7}};

The above is a two-dimensional array. When the two-dimensional array needs to be called and passed parameters, we need to pass its row number and column number together.

test(arr,3,5)//传参


void test (int arr[3][5],int r,int c)//传参调用

However, while using array pointers to simulate two-dimensional array parameter passing, we need to understand a knowledge.

The first address of the two-dimensional array is the address of the first row, and the two-dimensional array is composed of one-dimensional arrays.

Or it can be said that each row of a two-dimensional array can be expressed as an array address of a one-dimensional array.

 Then it can evolve into:

int arr0[5] = {1,2,3,4,5};

int arr1[5] = {2, 3, 4, 5, 6};

int arr2[5] = {3, 4, 5, 6, 7}; 

int arr3[] = {&arr0, &arr1, &arr2};

&arr0, &arr1, &arr2 represent the array address of each row in the two-dimensional array arr[ 3 ][ 5 ] respectively.

And we know that the two-dimensional array arr[ 3 ][ 5 ] where arr[ 3 ] is equivalent to an array name.

Therefore, the call by parameter can be written as:

void test ( int(*arr)[ 5 ], int r, int c )//传参调用

(* arr )[ 5 ] is equivalent to (*p)[ i ] not long ago, treating each row of the two-dimensional array as a one-dimensional array, and both arr and p are the addresses of the entire one-dimensional array, then two mean the same.

The difference is that (* arr ) represents the address of the first line, and when printing, it needs to traverse inside to reach the address of the second line and the third line.

Therefore, *(arr+i) represents the array name, representing arr[ i ] in arr[ i ][ j ] 

And in http://t.csdn.cn/c6Q6H , we learned a way of writing, arr[ i ] can be written as *(arr+i) where arr represents the array name.

So brought into this simulation, we can write arr[i][j] as *(*(arr+i)+j) where *(arr+i) and arr[i] are array names.

Finally, the parameter passing simulation of the two-dimensional array can be written as:

 

Guess you like

Origin blog.csdn.net/2301_76445610/article/details/132249066