[comp.std.c] struct declarations in dpANS C

pardo@june.cs.washington.edu (David Keppel) (10/21/88)

I have code of the form:

    typedef void (*rtl_fp)( struct save_t *, int n,  rtl_t *curr );
    typedef struct everyn_t {
	int		n;	
	rtl_fp	func;	
    } everyn_t;

In normal use the "struct save_t" is declared in another file that
depends on declarations in this (.h) file.  Even though struct save_t
is defined before the rtl_fp is used, this declaration still causes
the compiler to emit warnings about "argument passing between
incomopatible pointer types" unless I precede the function declaration
with

    struct save_t;

(In which case the compiler notes that there is an "empty
declaration".)  Question: is this proper behavior?

	;-D on  ( Prototypical Problematical Pterodactyl )  Pardo
-- 
		    pardo@cs.washington.edu
    {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo

karl@haddock.ima.isc.com (Karl Heuer) (10/22/88)

In article <6157@june.cs.washington.edu> uw-june!pardo (David Keppel) writes:
>I have code of the form:
>    typedef void (*rtl_fp)( struct save_t *, int n,  rtl_t *curr );

Identifiers declared within a prototype have very short scope; thus for
example `n' has a scope that extends only to the end of the prototype.
Similarly (and more subtly), `struct save_t' has this same short scope if it
hasn't been previously declared.  When you later declare `struct save_t' it
has a nonintersecting scope, and therefore is considered a different type.

>[the error occurs] unless I precede the function declaration with
>    struct save_t;

That's the correct way to do it; if the struct tag is already declared when
the prototype is encountered, the one in the prototype becomes a reference to
it instead of a new type.

>In which case the compiler notes that there is an "empty declaration".

That is incorrect.  The struct tag is being declared.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

henry@utzoo.uucp (Henry Spencer) (10/23/88)

In article <6157@june.cs.washington.edu> pardo@cs.washington.edu (David Keppel) writes:
>    typedef void (*rtl_fp)( struct save_t *, int n,  rtl_t *curr );
>... the "struct save_t" is declared in another file that
>depends on declarations in this (.h) file.  Even though struct save_t
>is defined before the rtl_fp is used, this declaration still causes
>the compiler to emit warnings about "argument passing between
>incomopatible pointer types" unless I precede the function declaration
>with
>    struct save_t;

This is correct.  save_t is not known at the first time it is used, i.e.
in the typedef for rtl_fp.  Therefore it is entirely proper, and indeed
highly desirable, for the compiler to object.  Adding "struct save_t;"
makes it a known, although incomplete, type.  The compiler's grumbling
about "struct save_t;" is improper; probably somebody forgot to fix
that when he added function prototypes.
-- 
The meek can have the Earth;    |    Henry Spencer at U of Toronto Zoology
the rest of us have other plans.|uunet!attcan!utzoo!henry henry@zoo.toronto.edu