Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: typedef a s...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 4 Topic 1079 of 1133
Post > Topic >>

Re: typedef a struct

by Kenneth Brody <kenbrody@[EMAIL PROTECTED] > Apr 18, 2008 at 04:11 PM

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.
 




 4 Posts in Topic:
typedef a struct
Taras_96 <taras.di@[EM  2008-04-16 16:56:29 
Re: typedef a struct
Kenneth Brody <kenbrod  2008-04-18 16:11:33 
Re: typedef a struct
=?utf-8?Q?Dag-Erling_Sm=C  2008-04-18 16:11:41 
Re: typedef a struct
Taras_96 <taras.di@[EM  2008-04-21 13:18:40 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 2:25:54 CDT 2008.