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

2022-05-15 05:17:57

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 = {
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 ： ## 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 */
``````

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`）.

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``*a``p``*p``int`1
The address of the first element `&a``a``&p``p``int*`0x9F36FAE8
The first i The value of the elements `a[i]``*(a+i)``p[i]``*(p+i)``int`3 (i = 2)
The first i Addresses of elements `&a[i]``a+i``&p[i]``p+i``int*`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) = &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``(*q)``**q``int`1
The address of the first element `&a``&(*q)``*q``int*`0x9F36FAE8
The first i The value of the elements `a[i]``(*q)[i]``*(*q+i)``int`3 (i = 2)
The first i Addresses of elements `&a[i]``&(*q)[i]``*q+i``int*`0x9F36FAF0 (i = 2)
Address of array `&a``q``int(*)`0x9F36FAE8

# 2. Two dimensional arrays and pointers

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

``````int a = {
{
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 ： But the actual storage form of two-dimensional array in memory is Store... In line order , As shown in the figure below ： 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 .

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``*a``int`{1, 2, 3}
Address of the first line `&a``a``int(*)`0x19AFFC48
The first i That's ok `a[i]``*(a+i)``int`{4, 5, 6} (i = 1)
The first i The address of the line `&a[i]``a+i``int(*)`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``*a``(*a)``**a``int`1
The address of the element in the first row and the first column `&a``a``*a``int*`0x19AFFC48
The first i The value of the element in the first column of the row `a[i]``*a[i]``(*(a+i))``**(a+i)``int`4 (i = 1)
The first i The address of the element in the first column of the row `&a[i]``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)``int`6 (i = 1, j = 2)
The first i Xing di j The address of the column element `&a[i][j]``a[i]+j``*(a+i)+j``int*`0x19AFFC5C (i = 1, j = 2)

## 2.2 Row pointer

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

``````int (*q) = a;  /*  Equivalent to q = &a */
``````

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

It can be seen from the grammar that , Row pointers are essentially pointers to arrays . Two dimensional array `a` First line `a` It's going to be a length of PI 3 One dimensional array of , So you can use its address `&a` 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`.

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``q``*q``int`{1, 2, 3}
Address of the first line `&a``a``&q``q``int(*)`0x19AFFC48
The first i That's ok `a[i]``q[i]``*(q+i)``int`{4, 5, 6} (i = 1)
The first i The address of the line `&a[i]``&q[i]``q+i``int(*)`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;
``````

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``p``*p``int`1
The address of the element in the first row and the first column `&a``&p``p``int*`0x19AFFC48
The first i Xing di j Value of column element `a[i][j]``p[i*3+j]``*(p+i*3+j)``int`6 (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+j``int*`0x19AFFC5C (i = 1, j = 2)

# 3. Reference resources

Two dimensional arrays and pointers ( Detailed explanation )