4

If I read this in a class as a data member, is the static referring to the entire expression (the array itself) or the elements of the array?

static const int* array[100];

Is the array, array, static, or does the array contain 100 static const int pointers ?

I'm assuming the former, but the way the word const changes meaning makes me wonder if static also changes meaning based on where in the expression it exists. In the above, the pointer array is not const yet the elements of the array are const, so I wonder if the pointer of the array is static.

3
  • you sure about that? const int* const array[100] would ensure a constant array Commented Jan 20, 2013 at 7:30
  • cprogramming.com/reference/pointers/const_pointers.html Commented Jan 20, 2013 at 7:35
  • @KhalidTaha see other answers; your comment is not accurate Commented Jan 20, 2013 at 7:38

2 Answers 2

4

C++11 7.1.1 Storage class specifiers:
§5

The static specifier can be applied only to names of variables and functions and to anonymous unions (9.5). There can be no static function declarations within a block, nor any static function parameters. A static specifier used in the declaration of a variable declares the variable to have static storage duration (3.7.1), unless accompanied by the thread_local specifier, which declares the variable to have thread storage duration (3.7.2). A static specifier can be used in declarations of class members; 9.4 describes its effect. For the linkage of a name declared with a static specifier, see 3.5.

So what are you declaring in the code example?

static const int* array[100];

Obviously, you are declaring the variable array So the static applies to array.
Do not confuse storage class specifier with cv-qualifiers the former applies to the variable being declared while the latter applies to the type.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Good to know about the difference between cv-quals and storage specifiers.
I'm normally against quoting standards. But when the question is about such a careful and (to some minds) subtle distinction, quoting the standard is the right thing to do.
2

static, extern and register apply to the variable you declare, to array in this example.

So, you get a static array of 100 pointers to constant integers.

3 Comments

By constant integers, you mean they can't be modified via the array's pointer, right?
note quite. probably more correct as "they can't be modified via the pointer in the array". they can't be modified via the array's pointer anyway because the array's pointer has no bearing on the pointers in the array.
@thang Nice, I knew the difference, but wrote it incorrectly, what I wanted to write was "pointers in the array" and English isn't my native language :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.