[comp.lang.c] typedefs that need each other

gwyn@smoke.brl.mil (Doug Gwyn) (01/12/91)

In article <26261@uflorida.cis.ufl.EDU> jdb@reef.cis.ufl.edu (Brian K. W. Hook) writes:
>typedef struct _struct1 {
>	STRUCT2 *structure;
>} STRUCT1;
>typedef struct _struct2 {
>	STRUCT1 *structure;
>} STRUCT2;

You cannot use an identifier as a typedef before the identifer IS a typedef!
However, you can use incomplete types in contexts such as this.  For instance:

struct _struct2;	/* optional in most cases, but recommended */
struct _struct1 {
	struct _struct2 *structure;
};
struct _struct2 {
	struct _struct1 *structure;
};
typedef struct _struct1 STRUCT1;
typedef struct _struct2 STRUCT2;

kaleb@thyme.jpl.nasa.gov (Kaleb Keithley) (01/16/91)

In article burley@geech.ai.mit.edu (Craig Burley) and by inference
jdb@reef.cis.ufl.edu (Brian K. W. Hook) wrote:
>   Scenario:
>
>   typedef struct _struct1 {
>	   .....
>	   STRUCT2 *structure;
>	   .....
>   } STRUCT1;
>
>   typedef struct _struct2 {
>	   .....
>	   STRUCT1 *structure;
>	   .....
>   } STRUCT2;
>

K&R2 in section 6.7 Typedef (on page 146) shows exactly how to do this 
with typedefs, and the above example would be:

    typedef struct _struct2 *STRUCT2ptr

    typedef struct _struct1 {
        .....
        STRUCT2ptr structure;
        .....
    } STRUCT1;

    typedef struct _struct2 {
 	   .....
 	   STRUCT1 *structure;
 	   .....
    } STRUCT2;


-- 
Kaleb Keithley                      Jet Propulsion Labs
kaleb@thyme.jpl.nasa.gov

Offensive quote coming soon to a .signature file near you.