Pages

Tuesday, October 10, 2017

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

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

The name of the two-dimensional array :

- Like a one-dimensional array, the name of a two-dimensional array represents the starting address value of the array. You can also use the operator to access the following address values. Note that in a two-dimensional array, the address value moves as the row unit.

- In a one-dimensional array, putting a '*' in front of an address value tells you the value stored at the address pointed to. In the case of a two-dimensional array, '*' must be appended two times to get the value. This is because the name of a two-dimensional array is a two-dimensional pointer.
(One-dimensional pointer to get value by pasting '*' once, two-dimensional pointer to get value by pasting twice)

- Let's look at an example.

Example Code

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

int main() {
     int arr[2][3] = {  {0,1,2},
                               {3,4,5} };

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

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

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

     cout << " arr          = " << arr << endl ;
     cout << " arr + 1      = " << arr + 1 << endl ;
     cout << endl;

     cout << " *arr         = " << *arr << endl ;
     cout << " *(arr + 1)   = " << *(arr + 1) << endl ;
     cout << endl;

     cout << " **arr        = " << **arr << endl ;
     cout << " **(arr + 1)  = " << **(arr + 1) << endl ;

          return 0;
}

- I declared a two-dimensional array "arr[2][3]" and gave it an initial value. Then we will print out the values and addresses stored in the array.

(1) 'arr' and 'arr + 1' were printed to see the value of the array name.
(2) Type '*'before the name and print it.
(3) Type '*' twice before the name and print it.

results :

      arr[0][0]      arr[0][1]      arr[0][2]
      arr[1][0]      arr[1][1]      arr[1][2]

                  0                  1                  2
                  3                  4                  5

 0x7ffd5b08f2c0 0x7ffd5b08f2c4 0x7ffd5b08f2c8
 0x7ffd5b08f2cc 0x7ffd5b08f2d0 0x7ffd5b08f2d4

 arr               = 0x7ffd5b08f2c0
 arr + 1         = 0x7ffd5b08f2cc

 *arr             = 0x7ffd5b08f2c0
 *(arr + 1)    = 0x7ffd5b08f2cc

 **arr           = 0
 **(arr + 1)  = 3

(1) Notice that the array name 'arr' represents the starting address value with a pointer. The result of using the address operator is (arr + 1) the start address of the next line, not the next address.

(2) When '*' is attached only once, it shows the address value just like the array name.

(3) If '*' is pasted twice, it means that it returns the value stored in the address pointed to.

No comments:

Post a Comment