Pointers to Incomplete Types in Prototypes

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


john at acorn.co.uk (John Bowler) writes:

>In article <700 at taumet.com> steve at taumet.com (Stephen Clamage) writes:
>>As shown, the prototype declares "struct bar" to be an incomplete type
>>*local to the prototype*, and hence unavailable outside the prototype.
>>It is then impossible to call this function, since no object of type
>>"struct bar" (or pointer to one) could ever exist.  (Any later "struct
>>bar" is a new type in a different scope.)

>This is incorrect; consider the following (the example is the complete
>compilation unit...):-

>extern void foo(struct bar *ptr);
>void bar(void *ptr) {
>	foo(ptr);
>}

True enough.  If you later define struct bar at, say, the file level,
you could call foo() by casting a pointer to void* first.


>Further it *is* possible for the argument to ``bar'' to genuinely be
>of the type ``struct bar *'' declared in the parameter list to foo:-

>struct bar {int i;};

>void foo(struct bar *ptr) {
>...
>}

Of course.  This is exactly what I said.

>Or how about the compilation unit:-
>void foo(struct bar *ptr) {             /* struct bar incomplete */
>	struct bar { int i; } mine;	/* Now it's complete. */
>...
>}

This does not solve the problem of there being no struct bar outside
function foo, as in the original example.  The struct bar INSIDE the
function is not the same type as any struct bar OUTSIDE the function,
either lexically before or after the function.  In fact, given the
sequence
	struct bar...	/* complete or incomplete */
	void foo(struct bar* p) { struct bar { ... } ... }
The struct bar inside the function is not the same type as that the
parameter p points to.

>But what about the following (the compiler I have rejects it;...

As it should.  It is illegal.

>void foo(struct bar *ptr) {		/* struct bar incomplete */
>	if (!ptr) {
>		struct bar { int i; } mine;/* Now it's complete. */
>		bar(&mine);
>	} else
>		ptr->i = 0;		/* It's now incomplete??? */
>}

You cannot complete an incomplete declaration in an outer scope inside
an inner scope.  If this is the compilation unit, there is no
complete declaration for what the parameter ptr points to.

You need to study a good C text on the subject of scopes.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.std.c mailing list