Pointers to Incomplete Types in Prototypes

John Bowler john at acorn.co.uk
Sat May 4 01:38:16 AEST 1991


In article <700 at taumet.com> steve at taumet.com (Stephen Clamage) writes:
>xor at aix01.aix.rpi.edu (Joseph Schwartz) writes:
>
>>More specifically, if struct bar has not been defined, is an ANSI
>>compiler allowed to reject the following prototype:
>> 
>>    extern void foo(struct bar *);
>>...
>There was another answer posted to this question which was not complete.
>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);
}

Or the code fragment:-

	...
	foo(0);
	...

You might get a warning from the latter, but it is valid ANSI C.

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) {
	if (!ptr) {
		struct bar mine;
		bar(&mine);
	} else
		ptr->i = 0;
}

Or how about the compilation unit:-

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

But what about the following (the compiler I have rejects it; it would seem
that the completion of the structure definition goes out of scope, even
though the (incomplete) structure definition is still in scope - is this
correct???)

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??? */
}

John Bowler (jbowler at acorn.co.uk)



More information about the Comp.std.c mailing list