Scope of parameters in the definition of a complex function

Consider the following obscure definition of a function that returns a pointer to an array of char

s:

char (*func(int var))[sizeof var]
{
    return 0;
}

      

Is it really?

The part of question is the use of the identifier var

in the expression sizeof

. At least according to GCC 4.9.2, it var

does not appear in the expression sizeof

. (Note that if, var

in the expression, sizeof is replaced with, say 42

, the code is valid and the question becomes uninteresting.)

However, in the C11 n1570 spec specification (the corresponding parts in C99 are the same, although possibly with a different subclass numbering), subclause 6.2.1 discusses identifier scopes and contains the following proposals that are relevant to this case:

6.2.1p4 contains:

If a declarator or type specifier that declares that an identifier appears inside a block or in a parameter declaration list in a function definition, the identifier has a block scope that ends at the end of the associated block.

1p7 contains (bolding mine):

Structure, union, and enumeration members have a scope that begins immediately after the tag appears in the type specifier that declares the tag. Each enumeration constant has a starts immediately after its defining enumerator appears in the list of enumerators. Any other identifier has a scope that begins immediately after its declaration completes.

It is clear that the declarator for var

appears inside the parameter declaration list in the function definition. Thus, according to 6.2.1p4, its scope ends at the end of the function body ("linked block"). Also, it var

clearly matches "any other identifier" specified in 6.2.1p7, so its scope starts immediately after its declarator ends, that is, at the end of the parameter list.

It seems to me that the spec says nothing about scales var

. Given that the spec does not say otherwise, the obvious (to me, anyway) interpretation of the "start" and "end" of the scope means that the scope covers the entire lexical range from start to end. So it seems like it var

really should be visible in the expression sizeof

.

Is there something in the spec that I didn't take into account? Is the notion of scope of an identifier implied to be interpreted in some other way than "continuous lexical spacing from the beginning of the scope to the end of the scope"? If so, how does this manifest in the specification?

+3


source to share


2 answers


The part is [sizeof var]

simply not part of the block, nor the declaration list, but the return type. Thus, the only identifiers that are visible are those with scope.



+2


source


The return type of a function is defined in the block scope or in the scope in which the function itself is declared. It does not belong to the large scope of the function definition block. In this area (where the function is declared), the function parameters have not yet been declared.

You can consider defining a function as



return type: char ( * )[sizeof var] // Oops..What is var?!
{
   // function block scope including its parameters declarations;
   int var;
}

      

+1


source







All Articles