current position:Home>[C + +] row pointer and column pointer of two-dimensional array

[C + +] row pointer and column pointer of two-dimensional array

2022-05-15 05:17:57zzy979481894

stay C++ in , Arrays are closely related to pointers . Any expression implemented by arrays and subscripts can be implemented equivalently by pointers and offsets . Let's first introduce the relationship between one-dimensional array and pointer , Then we introduce the row pointer and column pointer of two-dimensional array .

1. One dimensional arrays and pointers

Define a length of 3 One dimensional array of a

int a[3] = {
    1, 2, 3};

Assume that the first element address is 0x9F36FAE8, The schematic diagram of array elements in memory is shown in the following figure :
 Schematic diagram of one-dimensional array memory

1.1 A pointer to an array element

Pointers to array elements are declared and initialized as follows :

int* p = a;  /*  Equivalent to p = &a[0] */

Here is a declaration pointing to an array a Pointer to the first element of p.

Because in C++ in The array name is the address of the first element of the array , The array pointer name can be used to initialize directly , You can also explicitly take the address of the array element ( Such as &a[0]).

For arrays a And a pointer to its first element p, There are the following equivalent forms :

meaning Equivalent form type value
The value of the first element a[0]*ap[0]*pint1
The address of the first element &a[0]a&p[0]pint*0x9F36FAE8
The first i The value of the elements a[i]*(a+i)p[i]*(p+i)int3 (i = 2)
The first i Addresses of elements &a[i]a+i&p[i]p+iint*0x9F36FAF0 (i = 2)

* according to C++ Definition of address arithmetic operation , No matter what a Is it an array name or a pointer , There are

  • a[i] Equivalent to *(a+i)
  • &a[i] Equivalent to a+i

1.2 Pointer to array

The pointer to the array is declared and initialized as follows :

int (*q)[3] = &a;

Here is a declaration pointing to an array a In itself The pointer to q, therefore *q Equivalent to a.

Be careful : Array a The address of &a Numerically equal to the address of its first element a, But the two types are different .

For arrays a And a pointer to itself q, There are the following equivalent forms :

meaning Equivalent form type value
The value of the first element a[0](*q)[0]**qint1
The address of the first element &a[0]&(*q)[0]*qint*0x9F36FAE8
The first i The value of the elements a[i](*q)[i]*(*q+i)int3 (i = 2)
The first i Addresses of elements &a[i]&(*q)[i]*q+iint*0x9F36FAF0 (i = 2)
Address of array &aqint(*)[3]0x9F36FAE8

2. Two dimensional arrays and pointers

Define a size as 2×3 Two dimensional array of a

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

Assume that the first element address is 0x19AFFC48.

A two-dimensional array can be conceptually understood as a matrix :
 Schematic diagram of two-dimensional array matrix

But the actual storage form of two-dimensional array in memory is Store... In line order , As shown in the figure below :
 Schematic diagram of two-dimensional array memory

It can be seen that , Two dimensional arrays are stored in memory in a form similar to one-dimensional arrays , Elements a[i][j] The offset of can be calculated using the formula i* Number of columns +j Calculation .

Although two-dimensional arrays are not stored in the form of matrices , but Each row of a two-dimensional array is a one-dimensional array , At this point, it is consistent with the concept of matrix .

2.1 Address operation

Because a two-dimensional array is an array of a one-dimensional array , The element type is a one-dimensional array , The array name is still the address of the first element . ad locum “ First element ” Is the first line of a two-dimensional array , therefore a Is the address of the first line . Use array names a The following address operations can be performed .

2.1.1 That's ok

meaning Equivalent form type value
First line a[0]*aint[3]{1, 2, 3}
Address of the first line &a[0]aint(*)[3]0x19AFFC48
The first i That's ok a[i]*(a+i)int[3]{4, 5, 6} (i = 1)
The first i The address of the line &a[i]a+iint(*)[3]0x19AFFC54 (i = 1)

From this we can see that , Even if the type of array element becomes a one-dimensional array ,1.1 The final conclusion of this section is still valid .

2.1.2 Array elements

meaning Equivalent form type value
The value of the first row and first column elements a[0][0]*a[0](*a)[0]**aint1
The address of the element in the first row and the first column &a[0][0]a[0]*aint*0x19AFFC48
The first i The value of the element in the first column of the row a[i][0]*a[i](*(a+i))[0]**(a+i)int4 (i = 1)
The first i The address of the element in the first column of the row &a[i][0]a[i]*(a+i)int*0x19AFFC54 (i = 1)
The first i Xing di j Value of column element a[i][j]*(a[i]+j)(*(a+i))[j]*(*(a+i)+j)int6 (i = 1, j = 2)
The first i Xing di j The address of the column element &a[i][j]a[i]+j*(a+i)+jint*0x19AFFC5C (i = 1, j = 2)

2.2 Row pointer

The declaration and initialization of two-dimensional array row pointers are as follows :

int (*q)[3] = a;  /*  Equivalent to q = &a[0] */

Here is a declaration pointing to a two-dimensional array a Pointer to the first line q, therefore *q Equivalent to a[0].

It can be seen from the grammar that , Row pointers are essentially pointers to arrays . Two dimensional array a First line a[0] It's going to be a length of PI 3 One dimensional array of , So you can use its address &a[0] To initialize the line pointer q. On the other hand , Because the array name is the address of the first element , therefore a Equivalent to &a[0].

For two dimensional arrays a And a pointer to the first line q, There are the following equivalent forms :

meaning Equivalent form type value
First line a[0]q[0]*qint[3]{1, 2, 3}
Address of the first line &a[0]a&q[0]qint(*)[3]0x19AFFC48
The first i That's ok a[i]q[i]*(q+i)int[3]{4, 5, 6} (i = 1)
The first i The address of the line &a[i]&q[i]q+iint(*)[3]0x19AFFC54 (i = 1)

Expressions related to array elements are not listed here .

2.3 Column pointer

The declaration and initialization of two-dimensional array column pointers are as follows :

int *p = &a[0][0];

Here is a declaration pointing to a two-dimensional array a Pointer to the first row and first column elements p.

It can be seen that , Column pointers are ordinary pointers to array elements . A two-dimensional array can be treated as a one-dimensional array through a column pointer 、 Use the offset calculation formula to access the array elements .

For two dimensional arrays a And a pointer to its first row and first column elements p, There are the following equivalent forms :

meaning Equivalent form type value
The value of the first row and first column elements a[0][0]p[0]*pint1
The address of the element in the first row and the first column &a[0][0]&p[0]pint*0x19AFFC48
The first i Xing di j Value of column element a[i][j]p[i*3+j]*(p+i*3+j)int6 (i = 1, j = 2)
The first i Xing di j The address of the column element &a[i][j]&p[i*3+j]*p+i*3+jint*0x19AFFC5C (i = 1, j = 2)

3. Reference resources

Two dimensional arrays and pointers ( Detailed explanation )

copyright notice
author[zzy979481894],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/131/202205111245140433.html

Random recommended