Taras_96 wrote:
>
> Hi everyone,
>
> You can typedef a struct by using the following
>
> typedef struct x {} X, *PX;
>
> Now, I can understand the use of the typedefs, X and PX. They allow
> you to declare the structs without having to use the struct keyword.
> However, I'm not quite sure of the usefullness of the struct tag 'x'.
>
> I seem to remember someone telling me that it had something to do with
> resolving circular dependencies, when file A depends on file B, and
> vice versa. Also, it may have something to do with structs that refer
> to themselves (from the FAQ):
>
> 1.14: I can't seem to define a linked list node which contains a
> pointer to itself.
>
> A: Structures in C can certainly contain pointers to themselves;
> the discussion and example in section 6.5 of K&R make this
> clear. Problems arise if an attempt is made to define (and
> use)
> a typedef in the midst of such a declaration; avoid this.
>
> Could anyone shed some light on the usefulness of the struct tag 'x'?
Consider a simple case where your struct needs to contain a pointer
to another instance of itself, such as a linked list. You cannot
do something like this:
typedef struct
{
int x, y, z;
PX next;
}
X, *PX;
because "PX" does not yet exist when "PX next" is parsed. However,
this is perfectly valid:
typedef struct x
{
int x, y, z;
struct x *next;
}
X, *PX;
Also, you may have the need for structs which refer to each other,
such as parent-child relation****ps. For example, "struct x" needs
to contain a pointer to "struct y", and "struct y" needs to contain
a pointer to "struct x". Such forward references cannot use the
not-yet-defined typedef.
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@[EMAIL PROTECTED]
>
--
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
-- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.


|