I'm jumping into C++11 from C(ANSI). It's a strange world indeed.
I'm trying to come to grips with the following:
int tbl[NUM_ROWS][NUM_COLS] = { 0 };
for (auto row : tbl)
for (auto col : row) // ERROR - can't iterate over col (type int *)
// do stuff
The rational here, I'm guessing, is equivalent to the difference between (in C):
int v[] = { 1, 2, 3, 4, 5 };
int *u = v;
// (sizeof v) != (sizeof u);
However, I don't quite see how the following works:
int tbl[NUM_ROWS][NUM_COLS] = { 0 };
for (auto &row : tbl) // note the reference
for (auto col : row)
// do stuff
Logically, I'm thinking auto gets typed to int * const - thats what an "array" variable is, a constant pointer to a (possibly) non-constant element. But how is this any more iteratable than a normal pointer? Or, since row is a reference, it's actually typed to int [NUM_COLS], just as if we declared row as int row[NUM_COLS]?