Pointers to Incomplete Types in Prototypes

Bill Baxter baxter at clipper.ingr.com
Fri May 3 06:13:52 AEST 1991


In article <w!2g7+c at rpi.edu>, xor at aix01.aix.rpi.edu (Joseph Schwartz) writes:
|> Must an ANSI C compiler accept pointers to incomplete types in a
|> function prototype?
|>  
|> More specifically, if struct bar has not been defined, is an ANSI
|> compiler allowed to reject the following prototype:
|>  
|>     extern void foo(struct bar *);

No, the compiler should not reject it on the grounds that it is an incomplete
type.  First, 'bar' may have been defined previously with file scope, hence
this is an instance of that type.  Second, incomplete pointer types are
perfectly legitimate if all you are interested in is the pointer itself (i.e.,
you will not attempt to dereference what it points to without casting to a
known type).

Our compiler allows this construct.  Good luck with HP!

A more twisted case, and probably totally useless, is when you have
the following compilation module:

-----start f.c-------
int foo (struct s s) {
  struct s {
    int i;
  };
  return s.i;
}
-----end f.c---------

Since the identifiers in the prototype definition have function scope, should
this be legal ANSI C?  I can find no convincing verbiage in the standard that
would rule-out this case.  Our compiler currently does not accept this, but
I fear it should and we are currently seeking convincing arguments for why
we shouldn't have to implement it.

|> 
|> 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.  Can anyone give
|> me more conclusive information?  Our HP "ANSI" C compiler is complaining
|> about the above prototype, and I want some ammunition before I report
|> it to HP as a problem.

I think you have a legitimate gripe.  At the prototype declaration site, you
may have no need for the complete structure type.  Consider a module that is
manipulating pointers only, without regards to what is being pointed to.  Therefore, it would be sufficient to have an incomplete type.  You might be pulling pointers out of a data structure passing them to this function that might understand what is being pointed to.

|>  
|> Thanks in advance for any help you can provide.
|> 
|> 
|> -- 
|> Joe Schwartz
|> Internet: xor at mts.rpi.edu
|> Bitnet: userez3n at rpitsmts


-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Bill Baxter       Compiler Group: Advanced Processor Division      (415)852-2333
E-MAIL: {apple|pyramid}!clipper!baxter  USPS: 2400 Geng Rd., Palo Alto, CA 94303
_STD_DISC:  INGR has their opinions.  I have mine.  Believe them if conflicting.



More information about the Comp.std.c mailing list