🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2848

Re: Pointers to Incomplete Types in Prototypes

#2848
From: Stephen Clamage
Date: Sat, 04 May 1991 18:10
68 lines
2721 bytes
john@acorn.co.uk (John Bowler) writes:

>In article <700@taumet.com> steve@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@taumet.com
Technical Details
Message-ID: <708@taumet.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!taumet!steve
References: <w!2g7+c@rpi.edu> <700@taumet.com> <6831@acorn.co.uk>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!taumet!steve
From: steve@taumet.com (Stephen Clamage)
Newsgroups: comp.std.c
Subject: Re: Pointers to Incomplete Types in Prototypes
Message-ID: <708@taumet.com>
Date: 4 May 91 18:10:04 GMT
References: <w!2g7+c@rpi.edu> <700@taumet.com> <6831@acorn.co.uk>
Organization: Taumetric Corporation, San Diego
Lines: 68