2

When iterating through a multidimensional array like so:

int arr[2][2] = {{6, 7}, {8, 3}, {5, 2}};
for (auto &row : arr) {
    for (auto &cell : row) {
        // code
    }
}

What is the type of row and cell and why must you use a reference?

2 Answers 2

3

When you don't use a reference array-to-pointer conversion kicks in and row is of type pointer to an array of two ints (int(*)[2]). The inner loop is then ill-formed because for it to work you need either:

  • a type that has begin and end member functions that return iterators,
  • begin and end free functions that are found by argument dependent lookup and return iterators or
  • for array types, x and x + bound are used for begin and end, respectively, where x is the range and bound is the array bound.

int(*)[2] doesn't fullfile these conditons. OTOH, when you use auto&, you get int(&)[2] and the third bullet applies.

P.S. You've got too many initializers for int[2][2].

Sign up to request clarification or add additional context in comments.

1 Comment

Is the type of row int * or int(*)[2]? According to the book C++ primer, it says that the type of row is int *. Please refer to Using a Range for with Multidimensional Arrays
0

row is an array of ints. cell is a reference to an int. You need a reference if you want to modify the contents of cell, because otherwise the loop variable will be a copy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.