Victor Bazarov <v.********@comAcast.netwrote in message ...
Jess wrote:
Hello,
If I have a constant array, i.e. it's elements aren't changed, should
I declare it as:
const int a[] = {1,2,3};
or
int const a[] = {1,2,3}
Does not matter. Each element of 'a' is an int which is const.
For OP: <using loose wording>
The 'const' binds to it's left. If there is nothing to it's left, it is
allowed to bind to it's right (so old code don't break). So, the two lines
of code are identical.
>
For this simple problem, perhaps it doesn't matter. However, if I
have an array of pointers (e.g. array of strings), then I think there
are several ways to write with "const", but with different meanings:
(1). const char* const b[]
There is nothing to the left of the first 'const', so it binds to the 'char'
(not the '*'). The second 'const' binds to the '*' (not the 'b[]').
<reminder: this is very loose wording>
You could write (1) like:
char const * const b[] // same as above.
(2). char* const b[]
The 'const' binds to the '*' on it's left (not the 'char' or 'b[]').
(3). const char* b[]
There is nothing to the left of the 'const', so it binds to the 'char' (not
the '*').
Between my 'loose wording' and Victor's (Hi Victor.) explanation, I hope you
get a clearer picture. <G>
Just remember that the 'const' modifies the thing on it's *left* if
possible.
Try this:
const char const *zthing = "Hi there.";
You should get a compile time error (char gets 'const' twice).
// g++ error: duplicate `const'
--
Bob R
POVrookie