Article View: comp.std.c
Article #2840Re: Pointers to Incomplete Types in Prototypes
From: John Bowler
Date: Fri, 03 May 1991 15:38
Date: Fri, 03 May 1991 15:38
69 lines
2387 bytes
2387 bytes
In article <700@taumet.com> steve@taumet.com (Stephen Clamage) writes:
>xor@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@acorn.co.uk)
Technical Details
Message-ID:
<6831@acorn.co.uk>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!crdgw1!uunet!mcsun!ukc!acorn!johnReferences:
<w!2g7+c@rpi.edu> <700@taumet.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!crdgw1!uunet!mcsun!ukc!acorn!john From: john@acorn.co.uk (John Bowler) Newsgroups: comp.std.c Subject: Re: Pointers to Incomplete Types in Prototypes Message-ID: <6831@acorn.co.uk> Date: 3 May 91 15:38:16 GMT References: <w!2g7+c@rpi.edu> <700@taumet.com> Organization: Acorn Computers Ltd, Cambridge, UK Lines: 69