Structure tags

Stephen Clamage steve at taumet.com
Sat Feb 9 04:06:52 AEST 1991


t-itoh at utsm.nm.fujitsu.co.jp (Tsutomu Itoh) writes:

>1a.

>struct foo {int i;};
>struct foo;
>struct foo bar;

>1b.
>struct foo;
>struct foo {int i;};
>struct foo bar;

Both of these are ok.  In 1a there is no incomplete declaration.  In 1b,
line 1 is an incomplete declaration, completed in line 2, which is before
the complete declaration is needed.

>2a
>int bar(struct foo);
>struct foo {int i;};

This will not do what you want.  The scope of struct foo is the prototype
of function bar.  Any struct foo declared later, as in line 2, is a
different type from that in the prototype.  There is no way to pass an
argument to function bar, since no possible variable will be of a type
compatible with the struct foo in the prototype.  Not all compilers
get this right.  The prototype for bar is illegal in C++, by the way,
for exactly this reason.

>2b
>struct foo;
>int bar(struct foo);
>struct foo {int i;};

>Is the empty structure declaration necessary?

Some declaration is necessary for the reasons given above.  With a
declaration (complete or incomplete) for struct foo in scope when the
prototype for bar occurs, it is seen that bar takes an argument of that
same type struct foo.  In this case, the declaration of foo is completed
in line 3.

-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.std.c mailing list