2

I'm learning C++ and I have a doubt about arrays (I'm still studying the topic, some things are clear,while others still seem a bit obscure to me). I would like to know whether these declarations are legal and if so, how do they affect the newly created array:

let's assume I want to create an array of ints (to keep it simple) :

I know the following is legal

int array[size];

where size is a constant expression

and this

const int array[size];

this declaration means I can read-access the elements in the array but I cannot assign to them ( int a=array[2] is legal, array[1]=10 is an error ).

what about a "top level" const like

int const array[size];

or

const int const array[size];

are they legal in the first place? and if so, what do they mean? isn't an array constant by itself, being it impossible to assign to an array or to copy-initialize one?

thanks!

edit:

I know what "top level" and "low level" const means (though I learned that I can put the const qualifier before and after the base type specifier and that doesn't change anything). What I do want to know is how defining a const array ( and I mean not an array of const objects or variables) changes things.

Luca
  • 1,658
  • 4
  • 20
  • 41
  • 8
    `int const` and `const int` means exactly the same. See [Const before or const after?](http://stackoverflow.com/questions/5503352/const-before-or-const-after) – Bo Persson Aug 13 '15 at 06:41
  • Did you try it yourself? – Nishant Aug 13 '15 at 06:41
  • A good mnemonic is never to put `const` first and then `const` will be after what's constant (not that it's very useful here, but throw in a few asterisks and ampersand and you'll see were your heading). Putting `const` first is the same as putting `const` after `int`. – skyking Aug 13 '15 at 06:49

3 Answers3

5

The following two statements are valid and equal,

const int a[5]={1,2,3,4,5}; 

int const a[5]={1,2,3,4,5}; 

This is the way to initialize constant arrays.

The following declaration gives you a syntax error.

const int const a[5];
Deepu
  • 7,592
  • 4
  • 25
  • 47
  • so even if array are a compound type, putting const before or after the elements' type specifier doesn't change anything? is it the same as writing 'const int' or 'int const' ? – Luca Aug 13 '15 at 15:45
1

According to the C++ Standard qualifier const can be combined with any type specifier except itself. It means that in a sequence of type specifiers of a declaration there may be only one const qualifier. The type specifiers and qualifiers can be combined in any order.

So you may declare an integer constant array the following ways

const int array[size] = { /* list of initializers */ }; 
int const array[size] = { /* list of initializers */ }; 
const signed array[size] = { /* list of initializers */ }; 
signed const array[size] = { /* list of initializers */ }; 
signed const int array[size] = { /* list of initializers */ }; 
int const signed array[size] = { /* list of initializers */ }; 
const int signed array[size] = { /* list of initializers */ }; 
const signed int array[size] = { /* list of initializers */ }; 
int signed const array[size] = { /* list of initializers */ }; 
signed int const array[size] = { /* list of initializers */ }; 

All these array declarations are equivalent.

Take into account that constant objects shall be initialized when they are defined.

Qualifier const can be present also in declarators. Thus for example the follwoing declarations declare different types of objects

int const *p;
const int *p;

on the one hand and

int * const p = new int;
const int * const p = new( 10 );

on the other hand

The second qualifier const after * belongs to the declarator.

Also take into account that opposite to C++ in C qualifier const can be present several timed in the type specifier sequence. Redundant qualifiers are ignored.

So in C you may write for example

const signed const int const array[size] = { /* list of initializers */ }; 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

If you have pointer, it can be const pointer:

const int *ptr = 5;

or pointer to const value:

int* const ptr = 5;

or const pointer to const value:

const int* const ptr = 5;

Arrays are const pointers by definition - you cannot reassign whole array. Code below causes error:

int array[5];
int secondArray[5];
secondArray = array;

So define array const should not change array accesibility (it is const already). You cannot change accesibility of elements of array.

int array[5];

is similar (not the same!) to:

const int *array = malloc(sizeof(int)*5);

then code:

array[2] = 3;

is something like:

*(array+2) = 3;

Pointer is const but its value not.

Pingwin Tux
  • 124
  • 9
  • 1
    *"Arrays are const pointers by definition"* IMHO, this is misleading. Arrays are **not** pointers. Historically, they have evolved from something pointer-like, which is way assignment is still illegal IIRC (this prevented certain issues in pre-C legacy code). Also, the constness of an array is **per definition** the constness of their elements. `typedef int my_arr[10]; const my_arr x = {};`, then the type of `x` is `const int[10]`, and the type of its elements is `const int`. – dyp Aug 13 '15 at 07:14
  • @dyp: I think that on assembly level it could be pointers ;-) . – Pingwin Tux Aug 13 '15 at 17:23