forward references in typedefs (puzzling)

King Su wen-king at cit-vax.Caltech.Edu
Sun Jul 23 09:23:32 AEST 1989


In article <1196 at cbnewsl.ATT.COM> dfp at cbnewsl.ATT.COM (david.f.prosser) writes:

>The main advantage gained by separating the typedef declaration from the
<structure is that a single common header file can contain the typedefs
>while headers for particular modules can contain the complete structure.
<Only those parts of the program that need to know the members of the
>structure must include the detailed header.

I have often wondered about this.  Is it legal?  Since pointers for
different data objects are not required to have the same binary format
or even storage size, how would a compiler allocate space and initialize
a pointer of an unknown type?  Consider:

master_header.h:

    typedef struct s1 S1;
    typedef struct s2 S2;
    typedef struct s3 S3;

one_program_file.c:

    #include "master_header.h"

    struct s3 { S1 *pointer_s1;
		S2 *pointer_s2;
		int  a;
		....            } s;

When compiling "one_program_file.c", how does the compiler figure out
the size of 's' and the offset of 'a', given that 'pointer_s1' and
'pointer_s1' are of unknown size.  How does the compiler initialize
's' (assuming static variable) if the format for the pointers are
unknown.  Or is it true that pointers to structures are always
of the same size and format?

-- 
/*------------------------------------------------------------------------*\
| Wen-King Su  wen-king at vlsi.caltech.edu  Caltech Corp of Cosmic Engineers |
\*------------------------------------------------------------------------*/



More information about the Comp.lang.c mailing list