Semantics of declarators in C99

In accordance with ISO / IEC 9899: 1999 6.7.5 Β§2,

Each declarator declares one identifier and states that when the operand is of the same form, since the declarator appears in the expression , it denotes a function or object with the volume, storage duration and type specified by the declaration qualifiers.

I do not know why the expression appears in the semantics of declarators. Could you give some examples that will help me understand the meaning?

+6
source share
3 answers

Say you have an ad

int foo[42]; 

Part of the declarator foo[42] . Whenever something monotonous appears in an expression (or extension) (ie foo , followed by [ , followed by an expression, followed by ] ), the type of this subexpression will be declared as int .

In other words: as far as the syntax goes, type declaration

 int *bar; 

does not declare bar as int * , but instead declares *foo as int .

For a more active example, take a declaration

 float (*op[42])(float, float); 

In an expression, an operand of the same form might look like this:

 c = (*op[i])(a, b); 

According to the quote, the right side would be of type float .

It follows that

 *op[i] 

must have a function type (we ignore the fact that function constructors break down into corresponding types of pointers and calling a function via postfix () actually works with pointers, not pointers).

This, in turn, means that

 op[i] 

should indicate a pointer to a function and finally come to

 op 

denoting an array of function pointers, like that we can apply postfix [] on and return the correct type.

Fun, right?)

+4
source

Say what you declare:

 static int const i, j, k; 

The same as:

 static int const i; static int const j; static int const k; 

The declarator pointer, static int const applies to all identifiers.

You can also extend this logic to functions and function pointers.

 static int i, (*fun1)(void), fun2(void); 

which matches with:

 static int i; static int (*fun1)(void); static int fun2(void); 

As far as the part of the expression is shown, there is a comment in http://c0x.coding-guidelines.com/6.7.5.pdf . It says:

Semantics

Each declarator declares one identifier and states that when an operand of the same form appears in the expression as the declarator, it denotes a function or object with a scope, storage duration and type specified by the specification of the declaration.

A comment

The identifier form in the expression is likely to be the same as in the declarator. For example, the declarator * x will have this form in the expression when it is required to specify the value specified in x, and the declarator y [2] will have this form in the expression when the element from the array y is mentioned. This is the declarative part of the declaration that declares the identifier. There is a special type of declarator, an abstract-declarator that does not declare an identifier

I interpret the above:

If you declare:

 int *x; 

and use *x in the expression, type *x is int .

If you announce

 static int const *x; 

and use *x in the expression, type *x is static int const .

Sitelinks

Static Variable Declaration (C)

Is [] also a declarator (when used in a parameter declaration) in C?

C - access to declaration is not const const through declaration

+5
source

All that being said, given the type declaration

 const int a = 5; // declaration 

then the operand a in an expression of type

 x = a + b 

inside the same scope is of type const int .

0
source

Source: https://habr.com/ru/post/975896/


All Articles