Is there any way to use foreach in pointers? For example
int numbers[] = {1,2,3,4};
for (int& n : numbers) {
n *= 2;
}
this works fine but if I change numbers with int pointer foreach gives error.
int* numbers = new int[4];
*numbers = 1;
*(numbers + 1) = 2;
*(numbers + 2) = 3;
*(numbers + 3) = 4;
for (int& n : numbers) {
n *= 2;
}
delete[] numbers;
Aren't they both basically the same thing? I'm allocating 16 bytes of space for both, and it's obvious where they'll end up. Why does it work fine in the above example but not in the other example?