user694733
user694733

Reputation: 16043

Constant array of constant objects

How do you define constant array of constant objects in C (not C++)?

I can define

int const Array [] = {
    /* init data here */
};

but that is a non-constant array of constant objects.

I could use

int const * const Array = {
    /* init data here */
};

and it would probably work. But is it possible do this with array syntax? (Which looks more clean)

Upvotes: 5

Views: 8691

Answers (3)

masoud
masoud

Reputation: 56479

If you want the elements of array do not modify, just use this:

const int Array[];

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 477040

An array cannot be "constant" -- what is that even supposed to mean? The array size is already a compile-time constant in any case, and if all the members are constants, then what else do you want? What sort of mutation are you trying to rule out that is possible for a const int[]?

Upvotes: 12

Matteo Italia
Matteo Italia

Reputation: 126787

The "double constness" thing applies only to pointers because they can be changed to point to something else1, since the characteristics of arrays are statical by themselves (arrays cannot be changed in size/type/to point to something else) the only const you can apply is to their elements.


  1. so you have the variations "pointer to an int", "pointer to a constant int", "constant pointer to an int", "constant pointer to a constant int".

Upvotes: 8

Related Questions