Solved

Are values of array const or location of array const?

Posted on 2013-06-12
5
358 Views
Last Modified: 2013-06-12
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
Comment
Question by:naseeam
5 Comments
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 50 total points
ID: 39241790
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
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 75 total points
ID: 39241956
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
 
LVL 40

Accepted Solution

by:
evilrix earned 125 total points
ID: 39241975
>> 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
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 125 total points
ID: 39241979
Sorry Sara, seems we cross posted.
0
 

Author Closing Comment

by:naseeam
ID: 39242100
Question answered very well.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 134
How to fix  a C++ segmentation violation which occurs in a static member function? 22 102
Best book to learn C++ 4 70
c++, dynamic object by json 1 23
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now