Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Are values of array const or location of array const?

Posted on 2013-06-12
5
Medium Priority
?
365 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 200 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 35

Assisted Solution

by:sarabande
sarabande earned 300 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 500 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 500 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.
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.
Suggested Courses

715 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