In summary, anywhere in the declaration specifier (See section 7.1 in the ISO/IEC 14882-2012), ie before the *
. Qualifiers after the *
are associated with the pointer declarator, not the type specifier, and static
doesn't make sense within the context of a pointer declarator.
Consider the following cases:
You can declare a normal int and a pointer to an int in the same declaration list, like this:
int a, *b;
this is because the type specifier is int
, then you have two declarations using that type specifier int
, a
, and a pointer declarator *a
which declares a pointer to int
. Now consider:
int a, static b; // error
int a, *static b; // error
int a, static *b; // error
which should look wrong (as they are), and the reason (as defined in sections 7.1 and 8.1) is because C and C++ require that your storage specifiers go with your type specifier, not in your declarator.
So now it should be clear that that the following is also wrong, since the above three are also wrong:
int *static a; // error
Your last example,
typedef int* pointer;
static pointer j; // valid
pointer static k; // valid
are both valid and both equivalent because the pointer
type is defined as a type specifier and you can put your type specifier and storage specifeir in any order. Note that they are both equivalent and would be equivalent to saying
static int *j;
static int *k;
or
int static *j;
int static *k;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…