Q:

How do I use the for-each loop to scan through an int array object?

if I create a new int array object using new like below:
int *array = new int[20];

and fill the array with some integers, then attempting to scan through that array with a for-each loop will throw me an error:
for (int x : array)  // error

why does this happen and how can I fix it? I tried playing around with the referencer and address notations (* and &) but every combination I tried fails.
ex.
for (int &x : *array)  // does not work either.

A:

array is not an array, but a pointer, which can't be used for Range-based for loop, because there's no information about the size of the "array", the begin and end of the "array" can't be deduced.

range_expression  -   any expression that represents a suitable
  sequence (either an array or an object for which begin and end member
  functions or free functions are defined, see below) or a
  braced-init-list.

You might make it real array, (or use std::array instead of the raw array)
int array[20];
for (int x : array) { processing with x... }

Or write the loop manually:
for (int i = 0; i < 20; i++) { processing with array[i]... }

