Vivek Maran
Vivek Maran

Reputation: 2753

Declaration specifiers and declarators

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

Rules that I assume

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.

Upvotes: 2

Views: 14586

Answers (3)

KUNAL SAINI
KUNAL SAINI

Reputation: 3

Generally, the C "declarations" is like this declaration-specifiers declarators;

Here, "declaration-specifiers" comprises of type-specifiers , storage-class-specifiers and type-qualifiers .

"declarators" can be variables,pointers,functions and arrays etc..

errors like :- [Error] expected declaration specifiers or '...' before string constant This type of errors come ,reason for it is problem in declarations.

Upvotes: 0

AnT stands with Russia
AnT stands with Russia

Reputation: 320531

I'm not sure I full understand you first question. In terms of C++03 grammar const is a cv-qualifier. cv-qualifier can be present in decl-specifier-seq (as a specific kind of type-specifier), which is a "common" part of the declaration, as well as in init-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 *. A const specifier that precedes the first * is not considered a part of the individual declarator. This means that in this example

int const *a, *b;

const belongs to the left-hand side: decl-specifier-seq, the "common" part of the declaration. I.e. both a and b are declared as int const *. Meanwhile this

int *a, const *b;

is 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 to static int". You can declare a static pointer to int though

static int *p;

In this case the pointer itself is static, as you wanted it to be.

Upvotes: 4

ouah
ouah

Reputation: 145839

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.

Well, yes:

static T *a;

Declare a a pointer to T. a has static storage duration.

Upvotes: 0

Related Questions