4 Replies - 2137 Views - Last Post: 22 September 2012 - 04:43 AM Rate Topic: -----

#1 shards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-September 12

Range-Based For Loops with Multi-Dimensional Arrays

Posted 20 September 2012 - 12:02 PM

I am having trouble using a range-based for loop to traverse a multi-dimensional array. The problem occurs when I try to implement the inner loop using a ranged-based for loop. For example, the following code will compile and run without issue.

int *intArr[3][3] {{0}, {0}, {0}};

for (int** i : intArr){
    for (int j=0; j < 3; ++j){
        std::cout << i[j];
    }
}


However, when I try do the following:

int *intArr[3][3] {{0}, {0}, {0}};

for (int** arrayOuter : intArr){
    for (int* element : arrayOuter){
        std::cout << *element;
    }
}


The compiler spits out a very lengthy error, the beginning of which reads: loops.cpp:32:23: error: no matching function for call to 'begin(int**&)'
for (int* element : arrayOuter)


Now, my understanding is that intArr is a pointer to the the first element of the array, which itself is a pointer-to-int array. This means that for each iteration of the outer for loop the variable arrayOuter is initialized to a pointer-to-pointer-to-int, or simply stated: arrayOuter is an array-of-ints. This implies that each element of arrayOuter is a pointer-to-int. However, the compiler does not agree with my logic.

Thank you in advance for your feedback.

Is This A Good Question/Topic? 0
  • +

Replies To: Range-Based For Loops with Multi-Dimensional Arrays

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Range-Based For Loops with Multi-Dimensional Arrays

Posted 20 September 2012 - 01:54 PM

View Postshards, on 20 September 2012 - 08:02 PM, said:

Now, my understanding is that intArr is a pointer to the the first element of the array,
If that were the case then it wouldn't compile at all since std::begin and std::end aren't defined for pointers (they are defined for references to statically-sized arrays however)

The type of intArr is array-of-array-of-pointers-to-int, and arrayOuter could also have a type of int* (&)[3] (reference-to-array-of-pointers); something which is compatible with std::begin and std::end.

for (int* (&arrayOuter)[3] : intArr)
{
    for (int* element : arrayOuter)
    {
        std::cout << *element;
    }
}

Personally, i think that syntax is ugly. the auto keyword is nicer, and also far more generic:
for (auto& arrayOuter : intArr)
{
    for (auto& element : arrayOuter)
    {
        std::cout << *element;
    }
}


Link to the cppreference page describing all of the overloads for std::begin - http://en.cppreferen.../iterator/begin

This post has been edited by Bench: 20 September 2012 - 02:01 PM

Was This Post Helpful? 3
  • +
  • -

#3 shards  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-September 12

Re: Range-Based For Loops with Multi-Dimensional Arrays

Posted 21 September 2012 - 10:16 PM

Ahh...so my understanding was flawed. An array variable is not equivalent to a pointer variable. If anything, a reference to an array variable is somewhat equivalent to an array pointer. That is,

int anArray[10]{};
int* ptr_anArray = &anArray;



Thank you so much for your response.
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Range-Based For Loops with Multi-Dimensional Arrays

Posted 22 September 2012 - 04:37 AM

> An array variable is not equivalent to a pointer variable

An array is definitely not a pointer. People who say this did not understand the language.

Array expressions decay to pointers, as in you can't refer to an array directly, even though you can create an array. Consequently, this is correct:

int anArray[10];
int *p = anArray;



anArray is an array. It just decayed into &anArray[0] when you refer to it. You can't stop this decay.
Was This Post Helpful? 1
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Range-Based For Loops with Multi-Dimensional Arrays

Posted 22 September 2012 - 04:43 AM

View PostOler1s, on 22 September 2012 - 12:37 PM, said:

> An array variable is not equivalent to a pointer variable

An array is definitely not a pointer.
Indeed, as the C-FAQ says, the so-called equivalence of arrays and pointers refers to the syntax of arrays and pointers rather than anything specifically to do with the language constructs themselves - http://www.c-faq.com...ryptrequiv.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1