Pointers to Incomplete Types in Prototypes

Mike Vermeulen mev at hpfcso.FC.HP.COM
Tue May 7 10:56:08 AEST 1991


>    extern void foo(struct bar *);
>
> According to 3.1.2.5, struct bar is an incomplete type in this case,
> and struct bar * is a pointer to an incomplete type.
> 
> I couldn't find anything in 3.5.4.3 that allows or forbids incomplete
> types, or pointers to them, in function prototypes.

Agreed.  I don't think there is anything in the standard that forbids such
an incomplete type in a parameter list.

> Our HP "ANSI" C compiler is complaining about the above prototype

The HP ANSI C compiler issues the following warning:

	warning: struct 'bar' declared in parameter list will have scope
		 limited to this function declaration or definition.

I believe section 2.1.1.3 (see also footnote 6) allows a conforming
implementation to issue diagnostics such as the warning above, as long as it
correctly translates valid programs.

The reason for the warning is to help in the diagnosis of a situation such as:

	/* header file contents */
	extern void foo(struct bar *);
	struct bar { /* fields */ };

	/* user code */
	#include HEADER
	struct bar *barp;
	main(){
		foo(barp);
	}

In this program, the compiler will diagnose a mismatch in parameter types at
the call to foo().  The reason is that the first "struct bar" in the parameter
list goes out of scope at the end of the declaration; so the next declaration
of "struct bar" in the header file declares a new (incompatible) structure
type.

While the standard allows incomplete types to be declared, and go out of
scope at the end of the parameter list; this is usually not what the user
desires.  The warning was added to ease in diagnosing this problem.

> I want some ammunition before I report it to HP as a problem.

Consider it unofficially reported.  Contact me if you have further questions
or concerns about the HP ANSI C compiler.

--mev, mev at hpcomet.hp.com

Standard disclaimer: speaking for myself and not HP.



More information about the Comp.std.c mailing list