Solved

Are values of array const or location of array const?

Posted on 2013-06-12
5
357 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 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

747 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

13 Experts available now in Live!

Get 1:1 Help Now