"const" array

J

Jess

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.
 
V

Victor Bazarov

Jess said:
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
 
B

BobR

Victor Bazarov said:
Jess said:
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'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top