-3

I'm trying to have an array of pointers (int *a[10]) and then use a range based for loop (as in the C++11 standard). However, the compiler complains D: - it says "error: invalid initialization of reference of type ‘int&’ from expression of type ‘int*’".

My code is basically:

main(){
    int *a[10];
    for(int& e : a){}
}

I had also tried: //... for(int* e : a){}

But having neither, inside the loop body, *e = 1 or e = 1 works.

I've read this question which links me to this page; by reading that, I think I understand that the container you're trying to iterate over must implement a begin() and an end() methods. I guess the primitive arrays don't implement these; is that the case? Also, is it the case that int& a = c; makes a have the same address as c, for some int c?

Is there another alternative?

7
  • 1
    Or for(int *&e : a) {} if you want to access the pointers themselves. Commented May 27, 2015 at 9:57
  • 3
    Or for (auto e : a) {} / for (auto &e : a) {} Commented May 27, 2015 at 9:57
  • @AlexFarber I thought of that but it doesn't seem to be the behavior I want Commented May 27, 2015 at 9:58
  • @Quentin I do want to access them, but I don't understand how that works, then, because for(int *&e : a){*e = 1;} seems to do nothing, when I print it. Commented May 27, 2015 at 10:01
  • That's because you didn't initialize your pointers. Commented May 27, 2015 at 10:02

2 Answers 2

3

*e = 1 suggests that you want to make 10 pointers to 10 integers, each of which has the value 1.

First, you need to have a loop that references all the 10 pointers:

int* a[10];
for(int*& e : a) // Reference so you can modify a. 

Then, you need to create 10 pointers:

  e = new int;

and those have to be pointers to the value 1 so we complete the statement

 *e = 1;

or shorter

  e = new int (1);

When cleaning up, you'd have a similar loop:

 for (int& e : a) {
      delete e;
      e = nullptr;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

It works if I use for(int *&e : a) but not if I do for(auto e : a). Why is that? With the latter I get "173680127\n-1161788088" as the output.
That's because auto is deduced as int*. What happens then is that you modify a copy of the pointers in a. If you'd use auto& e: a, the deduced type of e would be int*& e. (In other words, the problem is not with auto but with leaving off the & reference.)
2

It's an array of int*, so of course you can't iterate over it using a variable of type int, that isn't the type of object in the array!

The objects in the array are int* so that's what your loop variable needs to be.

for (int* e : a) ...

Or

for (int*& e : a) ...

Or simply:

for (auto& e : a) ...

1 Comment

Right, I thought of the first one, of course, but it doesn't work as I want.

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.