forward references in typedefs
Doug Gwyn
gwyn at smoke.BRL.MIL
Fri Jul 21 08:56:21 AEST 1989
In article <55480 at tut.cis.ohio-state.edu> George M. Jones <george at cis.ohio-state.edu> writes:
>The following code compiles (Sun, GCC, Green Hills) just fine as expected:
> struct one
> {
> struct two_t *foo;
> } one_t;
> struct two
> {
> struct one_t *bar;
> } two_t;
If you were to change the member types to point to "struct two" and
"struct one" types, it would be correct. The lack of a diagnostic at
the end of the translation unit ("struct two_t and struct one_t missing
declarations") can be considered a compiler deficiency.
C deliberately allows forward struct references in such contexts.
It may not have been completely clear in K&R 1st Edition, but it's
officially specified in the proposed Standard.
>However all the compilers I tried appear to be choaking on the forward
>reference to two_t in the following chunk of code
> typedef struct one
> {
> two_t *foo;
> } one_t;
Sure; "two_t" is neither a C keyword nor a defined type at the point
that it is first used. The compiler cannot know that it is eventually
going to be a structure type, unless it were to scan ahead (which is
contrary to the intent of C).
You might consider writing something like:
typedef struct two two_t; /* incomplete type */
typedef struct one {
two_t *foo;
) one_t;
struct two { /* completes the type */
one_t *bar;
};
More information about the Comp.lang.c
mailing list