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?
for(int *&e : a) {}if you want to access the pointers themselves.for (auto e : a) {}/for (auto &e : a) {}for(int *&e : a){*e = 1;}seems to do nothing, when I print it.