What Bruce Eckel says about
int a=5;
int arr[a];
is that this should be
AVOIDED because int a=5 is not known to the compiler during compilation time while int arr[a] needs a to be known to the compiler during the compilation time.
SO, this ought not to work
does not mean it will never work. It simply means that your compiler is outshining the one he got in his time :)
about
const int a=5;
int arr[a];
Bruce Eckel says that,
any constant variable declared in the program is (
mostly)
not assigned the address and is treated as compile time constant (same way as our pre-processors do). So the requirement needed for declaration of array is fulfilled.
Now, problem of accessing a[7] is
not a fault. In words of Bjarne Stroustrup(page 92), taking a pointer to the element beyond the end of the array is
guaranteed to work. This is important for many algorithms. However, such a pointer does not in fact point to an element to the array, it
, may not be used for reading or writing. ( According to this, other elements as in above case can be altered.)
Also, the result of taking a element before array's first element is undefined and should be avoided.
Taking the pointer beyond array boundary is restricted as it may point to some address needed for other useful work by your program or computer and thus overwriting is can cause problems.
HTH.
Regards,
Prashant Bhutani
Junior Undergraduate
CSE, IT-BHU