A link that is not mentioned anywhere?

The following code snippet:

int ia[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 }; int(&row)[4] = ia[1]; 

enter image description here

I cannot understand why this code is valid. For my current understanding, I have no rational explanation. Can anyone help me with this? My problem is with &row , which seems to be not mentioned anywhere. My only explanation is that this should be fair since its initialization.

I have the following explanation from my book:

.... we define the string as a reference to an array of four ints

Which array? The one we're going to initialize?

+5
source share
3 answers
  int ia[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 }; 

It is an array of 3 elements, each element is an array of 4 integers.

 int(&row)[4] = ia[1]; 

Here, the int(&row)[4] declares a referenced named string that can refer to an array of 4 integers. = ia[1] initializes it with a reference to the second element in the ia array, which is an array of 4 integers.

+13
source
 int ia[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 }; 

ia is an array of 3 arrays of 4 int each.

ia[0] { 0, 1, 2, 3 } .

ia[1] { 4, 5, 6, 7 } .

ia[2] { 8, 9, 10, 11 } .

I don’t quite understand why you say that we will “initialize” it - at the moment, ia fully initialized.

 int(&row)[4] = ia[1]; 

Spiral rule (start with id ...):

row

... is an...

(&row)

... link ...

(&row)[4]

... into an array of four ...

int(&row)[4]

... int ...

= ia[1];

... ia[1] initialized (because you must initialize the link without bypassing it in any way).

So row now a reference to ia[1] , which is of type "array of four int ". That is all that is needed.

+6
source

(& row) [4] means that it can refer to an array of 4 elements, and ia [1] is a string from the 2d array ia [] [], which contains 4 elements, so it works fine.

0
source

Source: https://habr.com/ru/post/1235278/


All Articles