Array Pointers

#include<iostream>
#include<iomanip>

void main()
{      
  int i,j;
  int pole[3][3]={1,2,3,4,5,6,7,8,9};
  *(*(pole+2)+2)=0;

  for(i=0; i<3;i++)
  {
        for(j=0;j<3;j++)
        {
               cout << setw(5)<< pole[i][j];
        }
        cout << endl;
  }
}

This is my program, and the output I get is the following:

1 2 3

4 5 6

7 8 0

However, I am having trouble understanding what this line exactly means:

  *(*(pole+2)+2)=0;

In my understanding, this is a pointer to an array pointer, so basically, the first one we do:

*(pole+2)

which points to the second element of the array. Then

*(*(pole+2)+2)

which points to the 4th element of the array? It's right? If so, how can we change the last element [3] [3] to 0?

Thank.

+3
source share
2 answers

Here poleis a 2D array of 3 rows and 3 columns. Since the index of the array begins with 0, you assign pole[2][2] = 0, which actually means that it is 0assigned to the element of the 3rd row and 3rd column.

*(*(pole+2)+2) == *(pole[2] + 2) == pole[2][2]

+9
source

++, ,

2D-, int.

( + 2) 2 ( int):

2 * int (4 ) * 3 ( ) = 24

24 . 3- . :

3- + 2 * int (4 )

3- , 9. 0.

, . .

, .

+1

All Articles