With reference to the question Where in a declaration may a storage class specifier be placed? I started analyzing the concept of declaration-specifiers
and declarators
. Following is the accumulation of my understanding.
Declarations
- Generally, the
C
declarations follow the syntax ofdeclaration-specifiers declarators;
declaration-specifiers
comprises oftype-specifiers
,storage-class-specifiers
andtype-qualifiers
declarators
can be variables,pointers,functions and arrays etc..
Rules that I assume
declaration-specifiers
can be specified in any order, as an example- There cannot be more than a single
storage-class-specifier
- On the other hand there can be multiple
type-qualifiers
storage-class-specifier
shall not go with thedeclarator
Questions
Q1: In the declaration of a constant pointer, I see a mix of declarator
and type-qualifier
as below
const int *const ptr; //Need justification for the mix of declarator and type-specifier
Q2: There can be a pointer to static int
. Is there a possibility of providing the pointer a static
storage class? Means the pointer being static.
I'm not sure I full understand you first question. In terms of C++03 grammar
const
is acv-qualifier
.cv-qualifier
can be present indecl-specifier-seq
(as a specific kind oftype-specifier
), which is a "common" part of the declaration, as well as ininit-declarator-list
, which is a comma-separated sequence of individual declarators.The grammar is specifically formulated that a
const
specifier belonging to an individual pointer declarator must follow the*
. Aconst
specifier that precedes the first*
is not considered a part of the individual declarator. This means that in this exampleconst
belongs to the left-hand side:decl-specifier-seq
, the "common" part of the declaration. I.e. botha
andb
are declared asint const *
. Meanwhile thisis simply ill-formed and won't compile.
Your second question doesn't look clear to me either. It seems that you got it backwards. You claim that "there can be a pointer to
static int
"? No, there's no way to declare such thing as "pointer tostatic int
". You can declare a static pointer toint
thoughIn this case the pointer itself is static, as you wanted it to be.