Home c++ Two-dimensional array, pointers and output of the values ​​of the elements of...

Two-dimensional array, pointers and output of the values ​​of the elements of the array in C++

Author

Date

Category

Please explain why this is here in this code

# include & lt; bits / stdC++. H & GT;
Using Namespace STD;
INT MAIN ()
{
  INT ARR [] [3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  for (int i = 0; i & lt; 9; i ++)
  {
    COUT & LT; & LT; Arr [i] & lt; & lt; '\ t' & lt; & lt; SizeOF (Arr [i]) & lt; & lt; '\ n';
  }
}

The following values ​​are displayed:

0x61fdf0 12
0x61fdfc 12.
0x61Fe08 12.
0x61Fe14 12.
0x61Fe20 12.
0x61Fe2C 12.
0x61Fe38 12.
0x61Fe44 12.
0x61Fe50 12.

So why the address size of the variable two-dimensional array is 12 bytes, while the difference between adjacent addresses, for example, 0x61Fe14 and 0x61Fe20 is 6 bytes? Where did these numbers come from, if the integers occupy 4 bytes.

p.s. And why at all, if you take Arr [i] it is the address of the variable? That is, in the case of a one-dimensional array, if there is a variable Arr [i] , then its address is & amp; Arr [i] . Why the address of the variable was impossible to display only as & amp; Arr [i] [j] , why the second interpretation? Thanks in advance.


Answer 1, Authority 100%

Record

int arr [] [3] = {1, 2, 3, 4, 5, 6, 7, 8 , nine};

Equivalent entries

int arr [3] [3] = {{1, 2, 3}, {4, 5, 6 }, {7, 8, 9}};

Consequently, ARR There is a one-dimensional array of three items. And the elements of the array Arr themselves are one-dimensional arrays of three INT .

.

Thus, we have the following alignment:
Type ARR – This is int [3] [3]
Type Arr [i] – this is int [3]
Type Arr [i] [J] – this is int .

sizeof (arr) == 3 * SizeOF (int [3] == 3 * (3 * SizeOF ( int)) // == 3 * (3 * 4) if SizeOF (int) == 4

That is why the size of the addresses of the variable two-dimensional array is 12 bytes

Because Arr [i] is not the address of the variable two-dimensional array, but a one-dimensional array of three elements of type int . The sizeof operator used to the array returns the number of bytes occupied by an array.

And why at all, if you display ARR [i], it is the address of the variable?

Because there is no overloaded & lt; & lt; to output the interactions of the intention, but there is an operator to output const void * . And the array can be implicitly converted to the pointer to its first element, and the int * pointer may be implicitly converted to Const Void * .

Please note that if the Ar [i] array is converted to the pointer, then this is an int: int * . While the address of the array of & amp; Arr [i] has a pointer type to an array of three elements of the int: int (*) [3] .

In this case, the difference between neighboring addresses, for example, 0x61fe14 and 0x61Fe20 is 6 bytes?

Using letters as numbers in the record of you did not bother you? 😉 Prefix 0x signals that the numbers are written in a hexadecimal number system . You will see a more clearly difference by considering the difference between the second and first addresses:

0x61fdfc - 0x61fdf0 == 0xc == 12 (in decimal) == SizeOF (int [3]) == 3 * SizeOF (int)

Finally, since in the array of Arr there are only three elements, the cycle should be from zero to three:

for (int i = 0; i & lt; 3; i ++)

If you want to display the most nested int ‘s use two cycles:

for (int i = 0; i & lt; 3; i ++)
{
   for (int j = 0; j & lt; 3; j ++)
     COUT & LT; & LT; Arr [i] [j] & lt; & lt; "";
   COUT & LT; & LT; "\ n";
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions