The biggest difference between C + + pointer array and two-dimensional array

Time:2022-5-5

The following essay is about the description of pointer array and the biggest difference between pointer array and two-dimensional array.

Pointer array

  • The elements of the array are pointer type

Example: using pointer array to store matrix

1 #include 
 2 using namespace std;
 3 int main() {
 4     int line1[] = { 1, 0, 0 };    // First row of matrix
 5     int line2[] = { 0, 1, 0 };    // The second row of the matrix
 6     int line3[] = { 0, 0, 1 };    // The third row of the matrix
 7     
 8 // define an integer pointer array and initialize it
 9     int *pLine[3] = { line1, line2, line3 };    
10     cout << "Matrix test:" << endl;
11 // output matrix
12     for (int i = 0; i < 3; i++) {
13       for (int j = 0; j < 3; j++)
14         cout << pLine[i][j] << " ";
15     cout << endl;
16     }
17     return 0;
18 }
19 
20 
21 
22 the output result is:
23 Matrix test:
24 1,0,0
25 0,1,0
26 0,0,1

Comparison between pointer array and two-dimensional array

  • For the pointer array in the scale and the following two-dimensional array

1 int array2[3][3] ={ { 1,0,0 }, { 0,1,0 }, { 0,0,1 } };

The biggest difference between the two is the continuity of the storage order between the array rows. The row and row elements in the pointer array are not continuous in turn, while the two-dimensional array is stored in connection, as shown in the figure:

 

Recommended Today

Modify user information changeinfo

When judging the persistence layer: Problem: there is such a problem when modifying user information. For example: the user’s email is not required. It was not empty originally. At this time, the user deletes the mailbox information and submits it. At this time, if it is not empty to judge whether it needs to be […]