467,925 Members | 1,754 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,925 developers. It's quick & easy.

"const" array

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}

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[]
(2). char* const b[]
(3). const char* b[]

I think the first one means "b" is an array of pointers, each pointer
is pointing to a fixed (unchangable) address. Then for the "const
char*", does it mean the pointer is pointed to one single fixed char?
or, does it mean it points to an array of fixed chars (i.e. none of
the char is changable)?

The second and the third one are even more confusing. For example,
does the "const" in (2) mean the pointers pointing to fixed addresses,
or, the values pointed to are fixed?

Thanks a lot.

Apr 26 '07 #1
  • viewed: 20163
Share:
3 Replies
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 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[]
(2). char* const b[]
(3). const char* b[]
Those are three different declarations.
I think the first one means "b" is an array of pointers, each pointer
is pointing to a fixed (unchangable) address.
Pointer is not pointing to an address. Pointer is pointing to a char.
Now, each pointer is unchangeable, and that's defined by the "const"
closest to 'b'. Each 'char' to which every pointer points is _also_
unchangeable, and that's defined by the "const" before the 'char'.
Then for the "const
char*", does it mean the pointer is pointed to one single fixed char?
To one fixed char, to many fixed char... That means you cannot change
the 'char' _through_ that pointer. Whether you index it using 0 or
any other value does not matter.
or, does it mean it points to an array of fixed chars (i.e. none of
the char is changable)?
That's it.
The second and the third one are even more confusing. For example,
does the "const" in (2) mean the pointers pointing to fixed addresses,
or, the values pointed to are fixed?
The pointers are fixed. The chars can be changed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 26 '07 #2

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
Apr 26 '07 #3
Thanks a lot to both of you!
Jess

Apr 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Hans | last post: by
20 posts views Thread by liujiaping | last post: by
4 posts views Thread by Virtual_X | last post: by
3 posts views Thread by =?Utf-8?B?U3VybWVldCBKYW5kdQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.