4

There is a definition of function prototype scope (3.3.4/1 N3797):

In a function declaration, or in any function declarator except the declarator of a function definition (8.4), names of parameters (if supplied) have function prototype scope, which terminates at the end of the nearest enclosing function declarator.

Can we get an example described that rule?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 4
    Not sure what example you're looking for. A function declaration is a very simple construct. The function prototype scope means that those parameters are visible until the function declaration can be considered closed (hopefully I am not misconstruing the standard). That means, for example, that you cannot have two parameters with the same name. – Zaphod Beeblebrox Jun 24 '14 at 18:02
  • @AndreaLaforgia I'm looking for example clearly showing the scope. –  Jun 24 '14 at 18:09
  • @AndreaLaforgia This question is a perfect example of how the standard can complicate even the most simple of concepts. – jmstoker Jun 24 '14 at 18:11
  • @DmitryFucintv You should really update your profile saying that you are making a c++ compiler – yizzlez Jun 24 '14 at 18:23

2 Answers2

7

Here is a simple example

int a;

void f( int a, int a );

The compiler will issue an error for the second parameter a because its name coincides with the name of the first parameter. That is the compiler will report that there is a redefinition of the name a. The same name is defined twice in the same scope.

Or another example

struct A {};

void f( int A, struct A );

The first parameter name hides the structure name so the second parameter is defined using the elaborated name of the structure.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I think the first int a; in your example can be misleading. In order for the compiler to report a duplicate name in the prototype, you don't need to declare that variable. – Zaphod Beeblebrox Jun 24 '14 at 18:40
  • 1
    @Andrea Laforgia This shows that function prototypes have their own declaration regions and a name in a function prototype can hide a name in the outer declaration region. It would be better if I used the first object as a default argument of the function. But I had no much time that to write a vary detailed answer. – Vlad from Moscow Jun 24 '14 at 18:44
4

Here's an example that involves a relatively rare, but sometimes encountered mistake

void foo(struct S *v);

struct S {
  int i;
};

int main() {
  struct S *p = 0;
  foo(p);           // ERROR: incompatible pointer types
}

The above code is ill-formed (to borrow a C++ term), because struct S is not yet known at the point of declaration of foo. For this reason struct S inside the prototype of foo is interpreted as a forward declaration of a new type. It has function prototype scope. It goes out of scope once the prototype ends. This means that declaration of struct S inside the foo's prototype is completely unrelated to the declaration of struct S that follows it. These are two different types. Pointer p inside main is not compatible to the parameter type of foo. The code is ill-formed.

Note that if you swap the declaration of struct S and the prototype of foo, the declaration of struct S inside thew prototype is no longer a forward declaration. It is assumed to refer to the previously declared struct S. The code becomes correct.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765