• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

Are values of array const or location of array const?

class Parameters
{
    public: static ParameterInfo const Info[421];
};

Is the address where array is stored const or all 421 values of array are const?
0
naseeam
Asked:
naseeam
4 Solutions
 
HooKooDooKuCommented:
Look at the following for some detail:
http://www.cprogramming.com/tutorial/const_correctness.html

It seems to come down to this... with pointers, if const is before the pointer indicator, then what the pointer points to is const.  If the const is after the pointer indicator, then the pointer itself is const.

Combine that with the fact that an array variable is really just a pointer that points to the 1st element of an array, your const keyword appears before the pointer indicator (in this case the array square brackets).  So the array values are constant.

Of course you could also think of it this way.  An array is already a const pointer (i.e. you can't change what the array variable points to).  So if you add const to an array declaration, you are declaring the array contents as const.
0
 
sarabandeCommented:
Combine that with the fact that an array variable is really just a pointer that points to the 1st element of an array

no, an array variable is an array and not a pointer when it was defined as a variable or as a member variable.

class X
{
     std::string stringarray[10];
     ...

// x.cpp
...
int array[100];
...
void f()
{
    double doublearray[2];
    ....

Open in new window


all these 3 cases would define an array and not a pointer. you easily can check that by calling sizeof(array) which would return the real array size and not pointer size. you also cannot assign another pointer or NULL to the array variable as it would be with a pointer.

however, an array turns to a pointer when it was passed as argument to a function. within that function, it has the same behavior as it would be with a pointer pointing to 1st element as HooKooDooKu has described.

note, a static member of const anytype array[anynumber] needs an initialization in the cpp file. after that the array (elements) could not be updated because of the const.

Sara
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Combine that with the fact that an array variable is really just a pointer

Arrays are not pointers! They are different types entirely and the compiler knows this. The type of an array is T[N] whereas the type of a pointer to that array is T*. Arrays will decompose to pointers but they are not pointers.

eg.

int foo[5]; has a type int[5] it does not have a type of int * (or int const *)

The difference might seem semantic but I can assure you it isn't when you are writing generic (template) code!

http://www.cplusplus.com/forum/articles/8/
http://www.cplusplus.com/forum/articles/9/
http://www.cplusplus.com/forum/articles/10/

>> Is the address where array is stored const
The address space allocated to an array is fixed. It is a contiguous blob of memory and element zero will have the same address as the the array itself (since the start of the array and the address of element zero are the same thing, at least in terms of their address).

>> or all 421 values of array are const?
If you are refering to the memory locations that make up the array, they will be const (immutable) if the array is declared const. Regarding your example, yes the locations are const.

Tip if you read the definition from left to right it (sort of) says what it is.

 ParameterInfo    const           Info          [421]
|------type------||-const-| |-variable-||-array-|

- array variable of const types -
0
 
evilrixSenior Software Engineer (Avast)Commented:
Sorry Sara, seems we cross posted.
0
 
naseeamAuthor Commented:
Question answered very well.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now