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:57【zzy979481894】
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 :
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] 、*a 、p[0] 、*p | int | 1 |
The address of the first element | &a[0] 、a 、&p[0] 、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 toa+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] 、**q | int | 1 |
The address of the first element | &a[0] 、&(*q)[0] 、*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(*)[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 :
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 .
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] 、*a | int[3] | {1, 2, 3} |
Address of the first line | &a[0] 、a | int(*)[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+i | int(*)[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] 、**a | int | 1 |
The address of the element in the first row and the first column | &a[0][0] 、a[0] 、*a | int* | 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) | int | 4 (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) | 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)[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] 、*q | int[3] | {1, 2, 3} |
Address of the first line | &a[0] 、a 、&q[0] 、q | int(*)[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+i | int(*)[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] 、*p | int | 1 |
The address of the element in the first row and the first column | &a[0][0] 、&p[0] 、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 )
copyright notice
author[zzy979481894],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/131/202205111245140433.html
The sidebar is recommended
- Online FAQ positioning FAQ what is the cause of the high load problem?
- What is the function of getstatic, a common tool for online FAQs?
- Android 11 new soft keyboard occlusion / animation solution
- Common tools for online FAQs include?
- How does SAP commerce cloud configure new applications for storefront
- In the CMS GC process, what is the reason why the business thread puts objects into the old generation (the characteristics of concurrent collection)?
- How good and accurate is the recommendation?
- Online FAQ positioning FAQs what are the causes of continuous GC problems?
- Does the data reflect the real viewing experience?
- What are the reasons for fullgc (throw oom if FGC recovery is invalid)?
guess what you like
Algorithm improvement - basic algorithm (turtle speed multiplication)
[C + +] sword finger offer 10 - I. Fibonacci sequence
Online FAQ positioning FAQ nosuchmethodexception what is the cause of the problem?
IOS enables native im development
What is the common function of SM?
"Automated testing" a new generation of Web front-end automated testing framework - playwright, get started quickly!
Online FAQ positioning FAQ what is the cause of the high load problem?
What is the function of watch, a common tool for online FAQs?
Timeliness in recommender systems, Zhang Fuguo et al. ESWA 2017
Alibaba's open source Java diagnostic tool uses what methods to diagnose.
Random recommended
- What is the function of dashboard, a common tool for online FAQs?
- What is the role of JAD, a common tool for online FAQs?
- Online FAQ positioning FAQ what are the causes of high CPU utilization?
- 07 - explore the underlying principles of IOS | several OC objects [instance object, class object, metaclass], ISA pointer of object, superclass, method call of object and the underlying essence of class
- Extreme fox gitlab settled in Alibaba cloud computing nest to jointly improve the development experience on the cloud
- How does artificial intelligence help natural science
- Elementui upload file
- Modern CSS solution: CSS mathematical functions
- Create a general efficiency improvement solution for front desk + middle desk based on vue3 (network disk link)
- Brush 100 front-end high-quality interview real questions in 2 weeks, and the source code is complete
- Vue has reduced its workload by half since using components
- I built a front-end mock tool
- About uboot -- Ping virtual machine Ubuntu operation
- Video transcoder editready for Mac
- [taro] taro gets the detailed attributes of the element (solved)
- Picture and text difference comparison tool: kaleidoscope for Mac
- Background of spatiotemporal artificial intelligence
- The top 10 of oceanbase database competition was born, and the integration of industry and education accelerated the training of database talents
- China brand Day | Youxuan software: strengthen its own brand and fight for China's database industry
- New feature release of gaussdb (for redis): enhanced prefix scanning and multi rent isolation
- CICC purchases the original maintenance service of gbase database in 2022
- Java implementation sequence table
- Simple implementation of linked list in Java
- C + + parameterless constructor (difference between stack and heap)
- Vue NPM startup error - solution
- With the introduction of Alibaba cloud database into Shandong medical insurance information platform, the settlement response speed increased by nearly 10 times
- Yixinhuachen was selected into the atlas of relevant manufacturers in the primary market of China's big data industry
- 2021-06-05 Java Foundation (day four): two ways of locking
- Android bangs screen and water drop screen are the best overall adaptation scheme
- Don't take detours in Android learning
- Android realizes Gaode map track playback
- 2021 preparing for the 1000 Java interview questions that gold, silver and four must brush
- The database function queries the MySQL database at the correct time
- Linux changes the SSH connection mode, changes the public key to the user name and password, and logs in
- Websocket + springboot message active push
- Java common classes and methods
- Go connect to the database (simple query learning)
- HTTP - understand HTTP protocol (I)
- Spring boot quickly switches the configuration of development environment and production environment (application. YML)
- Java gets the date of the previous day