Robin2:
However if the system was consistent you would also be required to use &myArray when you want the address of the array. Using myArray with neither an & nor a subscript should cause an error.
That is how you get an address of an array!
But that is the problem, it is a pointer to an array not a pointer to the first element. If you used a subscript operator like (&myArray)[3] you are attempting to access an array of arrays. Each index moves the pointer the length of the entire array - and is valid in some circumstances (multidimensional arrays).
The biggest point to mention here is 'myArray' is not returning a pointer to the variable, but an element - a closed concept only for arrays. '&myArray' and '&myInt' are both returning pointers to the variable you are using. Which is consistent use of the address-of operator.
If you dereference the pointer returned by '&myArray' and '&myInt' you return to the original variable. Whereas dereferencing the pointer provided by 'myArray' you get a reference to an element, which has nothing to do with the array.
If the rules you mentioned were in place, there would be no way to access the elements of a variable without some ugly code which converts the array pointer to a primitive pointer, then using pointer arithmetic to move along raw bytes.
((char*)(&myArray))[ 4 ];
This method is quite ugly, and breaks the strict typing elements. It allows you to completely disregard the type of the array, and has no size information associated with it. The array might not even be a char type.
And as for consistency, the way array's are accessed in C++ is consistent with 90% of other languages that use arrays. The parts that differ are because C++ has the concept of pointers, which many languages do not.
Another point is in reality, 'myArray' and 'myInt' are both expressions returning a reference to themselves. I think what you were saying is inconsistent, is actually there, it is just not as obvious. The code below shows this:
int array[10];
int (&arrRef)[10] = array;
int value;
int &valRef = value;
The reason why an array can cast is because it has a built in conversion. This concept can be described with a struct:
The code below will not work because a pointer and object are not compatible.
struct Foo{
char *ptr;
};
void setup() {
Foo foo;
char *ptr = foo;
}
void loop(){}
However adding a conversion operator, like what arrays have built in, then it can:
struct Foo{
//Conversion operator
operator char*(){ return ptr; }
char *ptr;
};
void setup() {
Foo foo;
char *ptr = foo;
}
void loop(){}