Pages

Saturday, October 14, 2017

Easy tutorial for C / C ++ - Arrays 7 (The name of the two-dimensional array 2)

Easy tutorial for C / C ++ - Arrays 7 (The name of the two-dimensional array 2)

- The name of the two-dimensional array is a two-dimensional pointer, and you can access the value stored in the address by pasting '*' twice. 

- In the previous, we only approached the first of each row. Let's look at finding the element values of the entire array.

How to access specific elements.

'Array name' ['row'] ['column']

Address value
  : * ('array name' + 'row number') + 'column number'

The value assigned to the address
  : * (* ('array name' + 'row number') + 'column number')

- Let's look at an example.

Example Code

#include <iostream>
#include <iomanip>
using namespace std;

int main() {

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

     for(int i = 0; i < 2; i++){
                   for(int j = 0; j < 3; j++){
              cout << setw(15) << a[i][j] ;
                   }
                   cout << endl;
          }
         cout << endl;

     for(int i = 0; i < 2; i++){
                   for(int j = 0; j < 3; j++){
              cout << setw(15) << &a[i][j] ;
                   }
                   cout << endl;
          }
         cout << endl;

     cout << " The name of the two-dimensional array 2" << endl;
     cout << endl;
     cout << "Addresses of the 2-D array" << endl;

     for(int i = 0; i < 2; i++){
                   for(int j = 0; j < 3; j++){
              cout << setw(15) << *(a + i) + j ;
                   }
                   cout << endl;
          }
         cout << endl;

     cout << "Values of the 2-D array " << endl;

     for(int i = 0; i < 2; i++){
                   for(int j = 0; j < 3; j++){
              cout << setw(15) << *(*(a + i) + j) ;
                   }
                   cout << endl;
          }
         cout << endl;

          return 0;
}

- The two-dimensional array a[2][3] was declared and initial values were given. We used the indexes of rows and columns to print the values and addresses stored in the array.

- Let's print the address using the name of the array. I have written the sentence "* (a + i) + j" in the for loop. Where 'i' and 'j' are the index representing the row and the column, respectively.

- The whole sentence "* (a + i) + j" is enclosed in parentheses "* ()" to get the value stored in the address. "* (* (a + i) + j)"

results :

                         0                          1                          2
                         3                          4                          5

 0x7ffd4024ebe0  0x7ffd4024ebe4   0x7ffd4024ebe8
 0x7ffd4024ebec  0x7ffd4024ebf0   0x7ffd4024ebf4

 The name of the two-dimensional array 2

Addresses of the 2-D array
 0x7ffd4024ebe0  0x7ffd4024ebe4  0x7ffd4024ebe8
 0x7ffd4024ebec  0x7ffd4024ebf0  0x7ffd4024ebf4

Values of the 2-D array
                         0                          1                          2
                         3                          4                          5

- You can see that you can obtain the same results.

No comments:

Post a Comment