One might hope to think of Line 1 as creating a reference for the 0th entry of foo
. However, that's not how C++ works. Rather, the assignment operator (=
) is invoking a copy operation. So, C++ is interpreting Line 1 as a directive to copy the elements of foo[0]
into a new array inappropriately named foo_alias_compile_error
.
This isn't what was meant -- one wanted a reference, not a copy. So, it's good that C++ happens to have caused an error for an unrelated reason and saved one from oneself.
A viable solution is suggested by @FrançoisAndrieux. Here's a fuller example showing that a reference (not a copy) can be made with int (&foo_reference)[10] = foo[0];
.
int foo[10][10];
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
foo[i][j] = i + j * 10;
}
}
int (&foo_reference)[10] = foo[0];
for (int j = 0; j < 10; ++j) {
foo_reference[j] = 100 + j;
}
for (int i = 0; i < 10; ++i) {
printf("foo [0][%i] is %i\n", i, foo[0][i]);
printf("foo_refer[%i] is %i\n", i, foo_reference[i]);
}
Output Snippet
foo [0][0] is 100
foo_alias[0] is 100
foo [0][1] is 101
foo_alias[1] is 101
foo [0][2] is 102
foo_alias[2] is 102
foo [0][3] is 103
foo_alias[3] is 103
Side-note
It's worth mentioning that functions that take arrays as arguments implicitly convert their array arguments into pointers (as is shown in Line 3). So, that's one reason why one might incorrectly think something like Line 1 should work.
In other words, the following code
void barz(int arg[]) {
arg[2] = 99999;
}
int another_array[] = {0, 1, 2, 3, 4};
barz(another_array);
printf("another_array[2] is %i\n", another_array[2]);
"Correctly" prints 99999
, not 2
.