Pointers to Incomplete Types in Prototypes

Stephen Clamage steve at taumet.com
Sun May 5 04:28:39 AEST 1991


xor at aix01.aix.rpi.edu (Joseph Schwartz) writes:

>But I still have a hard time believing that it applies to struct bar in:

>     extern void foo(struct bar *);

>Making bar have local scope in this case is of questionable usefulness,
>and it would appear to break code that existed before the Standard.

I agree it is not useful to write code this way.  But code before the
standard did not use prototypes -- they were introduced by the standard
(borrowing from C++).  So what previously-correct code can be broken?

The only alternative is to promote the declaration in a function
prototype to some outer scope.  To which scope?  Always to file scope?
Just to the enclosing scope?  Consider:
	void f1()
	{
	    extern void f2( void (*)(struct bar*) );
	    ...
	}
Here, f2 takes a parameter which is a pointer to a function which takes
a parameter of type struct bar.  If no declaration for struct bar
appears before this, what is the scope of struct bar?

The ANSI rules provide a completely consistent interpretation of these
issues.  In the absence of a corresponding declaration in an enclosing
scope, the scope of a declaration in a prototype parameter list ends
with the prototype.  If you mean for it to refer to a declaration in an
enclosing scope, the declaration must be visible.  This too is a uniform
rule: you must declare a type before you can use it.
 
>Then why does this:
>      struct bar;
>      extern void foo(struct bar *);
>solve the scoping problems?

Because of the uniform scoping rules throughout the standard.  The first
declaration of struct bar is in a scope which encloses the prototype
scope, and there is no other declaration to hide it.  Therefore, the
struct bar in the prototype must refer to the same type as the first.

The point you seem to be missing is that the parameter list in a function
prototype not part of a function definition has its own scope.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.std.c mailing list