Solved

Are values of array const or location of array const?

Posted on 2013-06-12
5
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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
 
LVL 1

Author Closing Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
getting a fire fox page (view page source ) in c++ 6 48
best sources to up-to-date in C++? 8 115
Dynamically allocate memory 9 76
Which Linux flavors will this run on? 6 116
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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

752 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