question on structures.

Discussion in 'C Programming' started by broli, Mar 26, 2008.

  1. broli

    broli Guest

    typedef struct y
    {
    x1 *c;
    x2 *c;
    } y;

    typedef struct x1
    {
    y *a;
    }x1;

    typedef struct x2
    {
    y *b;
    }x2;


    ^^ If I want to have declarations like that in my program, then what
    should I do ? Would it work if I put these two lines before the
    declaration of y -

    typedef struct x1 x1;
    typedef struct x2 x2;
     
    broli, Mar 26, 2008
    #1
    1. Advertising

  2. broli <> wrote:
    > typedef struct y
    > {
    >   x1 *c;
    >   x2 *c;
    > } y;
    >
    > typedef struct x1
    > {
    >   y *a;
    > }x1;
    >
    > typedef struct x2
    > {
    >   y *b;
    >
    > }x2;
    >
    > ^^ If I want to have declarations like that in my
    > program, then what should I do ?


    Read the FAQ.

    http://www.c-faq.com/decl/mutrefstructs.html

    > Would it work if I put these two lines before the
    > declaration of y -
    >
    > typedef struct x1 x1;
    > typedef struct x2 x2;


    Yes, but you don't need the typedefs...

    struct y
    {
    struct x1 *c;
    struct x2 *c;
    };

    struct x1
    {
    struct y *a;
    };

    struct x2
    {
    struct y *b;
    };

    --
    Peter
     
    Peter Nilsson, Mar 26, 2008
    #2
    1. Advertising

  3. broli

    broli Guest

    On Mar 26, 10:56 am, Peter Nilsson <> wrote:
    > broli <> wrote:
    > > typedef struct y
    > > {
    > > x1 *c;
    > > x2 *c;
    > > } y;

    >
    > > typedef struct x1
    > > {
    > > y *a;
    > > }x1;

    >
    > > typedef struct x2
    > > {
    > > y *b;

    >
    > > }x2;

    >
    > > ^^ If I want to have declarations like that in my
    > > program, then what should I do ?

    >
    > Read the FAQ.
    >
    > http://www.c-faq.com/decl/mutrefstructs.html
    >
    > > Would it work if I put these two lines before the
    > > declaration of y -

    >
    > > typedef struct x1 x1;
    > > typedef struct x2 x2;

    >
    > Yes, but you don't need the typedefs...
    >
    > struct y
    > {
    > struct x1 *c;
    > struct x2 *c;
    > };
    >
    > struct x1
    > {
    > struct y *a;
    > };
    >
    > struct x2
    > {
    > struct y *b;
    > };
    >


    Why is it wrong to use typedef here ? Or are you trying to say that if
    I precede the struct x1 and x2 declarations with following two
    statements -
    typedef struct x1 x1;
    typedef struct x2 x2;

    Then I don't need to add the typedef keyword again when declaring the
    structs ?
     
    broli, Mar 26, 2008
    #3
  4. broli

    broli Guest

    Specifically, this is my problem -

    typedef struct VertexData VertexData;
    typedef struct EdgeData EdgeData;
    typedef struct TriangleData TriangleData;

    typedef struct HalfData{
    HalfData *next;
    HalfData *previous;
    HalfData *next;
    VertexData *origin;
    TriangleData *left;
    EdgeData *edge;
    }HalfData;

    struct VertexData{
    HalfData* half;
    };

    struct EdgeData{
    HalfData* half;
    };

    struct PolygonData{
    HalfData* half;
    };

    Have I used a wrong notation over here ?
     
    broli, Mar 26, 2008
    #4
  5. On 26 Mar, 06:09, broli <> wrote:
    > On Mar 26, 10:56 am, Peter Nilsson <> wrote:
    > > broli <> wrote:


    > > > typedef struct y
    > > > {
    > > >   x1 *c;
    > > >   x2 *c;
    > > > } y;

    >
    > > > typedef struct x1
    > > > {
    > > >   y *a;
    > > > }x1;

    >
    > > > typedef struct x2
    > > > {
    > > >   y *b;

    >
    > > > }x2;

    >
    > > > ^^ If I want to have declarations like that in my
    > > > program, then what should I do ?

    >
    > > Read the FAQ.

    >
    > >http://www.c-faq.com/decl/mutrefstructs.html

    >
    > > > Would it work if I put these two lines before the
    > > > declaration of y -

    >
    > > > typedef struct x1 x1;
    > > > typedef struct x2 x2;

    >
    > > Yes, but you don't need the typedefs...

    >
    > >   struct y
    > >   {
    > >     struct x1 *c;
    > >     struct x2 *c;
    > >   };

    >
    > >   struct x1
    > >   {
    > >     struct y *a;
    > >   };

    >
    > >   struct x2
    > >   {
    > >     struct y *b;
    > >   };

    >
    > Why is it wrong to use typedef here ?


    he's not saying the typedefs are "wrong" he's saying
    they are unnecessary. Many C programmers don't like
    typedefing structs. Many do. It's a style thing.
    (I'm a typedefer myself, I thing making struct
    part of the type name is ugly).


    > Or are you trying to say that if
    > I precede the struct x1 and x2 declarations with following two
    > statements -
    > typedef struct x1 x1;
    > typedef struct x2 x2;
    >
    > Then I don't need to add the typedef keyword again when declaring the
    > structs?


    you can declare structs without using typedef.


    --
    Nick Keighley
     
    Nick Keighley, Mar 26, 2008
    #5
  6. broli

    santosh Guest

    broli wrote:

    > Specifically, this is my problem -
    >
    > typedef struct VertexData VertexData;
    > typedef struct EdgeData EdgeData;
    > typedef struct TriangleData TriangleData;
    >
    > typedef struct HalfData{
    > HalfData *next;
    > HalfData *previous;
    > HalfData *next;


    This is a duplicate member.

    > VertexData *origin;
    > TriangleData *left;
    > EdgeData *edge;
    > }HalfData;


    I would define the above as a plain struct declaration and later typedef
    it.

    struct HalfData {
    /* ... */
    };

    typedef struct HalfData HalfData;

    Also I don't like the practise of giving same names for both the struct
    tags and the corresponding typedef, but that is a style issue.

    > struct VertexData{
    > HalfData* half;
    > };
    >
    > struct EdgeData{
    > HalfData* half;
    > };
    >
    > struct PolygonData{
    > HalfData* half;
    > };
    >
    > Have I used a wrong notation over here ?


    Yes, for HalfData declaration.
     
    santosh, Mar 26, 2008
    #6
  7. broli

    broli Guest

    On Mar 26, 2:48 pm, santosh <> wrote:
    > broli wrote:
    > > Specifically, this is my problem -

    >
    > > typedef struct VertexData VertexData;
    > > typedef struct EdgeData EdgeData;
    > > typedef struct TriangleData TriangleData;

    >
    > > typedef struct HalfData{
    > > HalfData *next;
    > > HalfData *previous;
    > > HalfData *next;

    >
    > This is a duplicate member.
    >
    > > VertexData *origin;
    > > TriangleData *left;
    > > EdgeData *edge;
    > > }HalfData;

    >
    > I would define the above as a plain struct declaration and later typedef
    > it.
    >
    > struct HalfData {
    > /* ... */
    > };
    >
    > typedef struct HalfData HalfData;
    >
    > Also I don't like the practise of giving same names for both the struct
    > tags and the corresponding typedef, but that is a style issue.
    >
    > > struct VertexData{
    > > HalfData* half;
    > > };

    >
    > > struct EdgeData{
    > > HalfData* half;
    > > };

    >
    > > struct PolygonData{
    > > HalfData* half;
    > > };

    >
    > > Have I used a wrong notation over here ?

    >
    > Yes, for HalfData declaration.


    Ok so I take your suggestions -

    typedef struct VertexDataStruct VertexData;
    typedef struct EdgeDataStruct EdgeData;
    typedef struct TriangleDataStruct TriangleData;

    struct HalfDataStruct{
    struct HalfDataStruct *next;
    struct HalfDataStruct *previous;
    struct HalfDataStruct *next;
    };

    typedef struct HalfDataStruct HalfData;

    ( I still don't see how this is different from what i did previously
    ie typedef struct HalfData{
    ......}Halfdata;)

    struct VertexDataStruct{
    ....
    };

    struct EdgeDataStruct{
    ....
    };

    struct TriangleDataStruct{
    ....
    };
     
    broli, Mar 26, 2008
    #7
  8. broli

    broli Guest

    On Mar 26, 3:47 pm, broli <> wrote:
    > On Mar 26, 2:48 pm, santosh <> wrote:
    >
    >
    >
    > > broli wrote:
    > > > Specifically, this is my problem -

    >
    > > > typedef struct VertexData VertexData;
    > > > typedef struct EdgeData EdgeData;
    > > > typedef struct TriangleData TriangleData;

    >
    > > > typedef struct HalfData{
    > > > HalfData *next;
    > > > HalfData *previous;
    > > > HalfData *next;

    >
    > > This is a duplicate member.

    >
    > > > VertexData *origin;
    > > > TriangleData *left;
    > > > EdgeData *edge;
    > > > }HalfData;

    >
    > > I would define the above as a plain struct declaration and later typedef
    > > it.

    >
    > > struct HalfData {
    > > /* ... */
    > > };

    >
    > > typedef struct HalfData HalfData;

    >
    > > Also I don't like the practise of giving same names for both the struct
    > > tags and the corresponding typedef, but that is a style issue.

    >
    > > > struct VertexData{
    > > > HalfData* half;
    > > > };

    >
    > > > struct EdgeData{
    > > > HalfData* half;
    > > > };

    >
    > > > struct PolygonData{
    > > > HalfData* half;
    > > > };

    >
    > > > Have I used a wrong notation over here ?

    >
    > > Yes, for HalfData declaration.

    >
    > Ok so I take your suggestions -
    >
    > typedef struct VertexDataStruct VertexData;
    > typedef struct EdgeDataStruct EdgeData;
    > typedef struct TriangleDataStruct TriangleData;
    >
    > struct HalfDataStruct{
    > struct HalfDataStruct *next;
    > struct HalfDataStruct *previous;
    > struct HalfDataStruct *next;
    > };
    >
    > typedef struct HalfDataStruct HalfData;
    >
    > ( I still don't see how this is different from what i did previously
    > ie typedef struct HalfData{
    > .....}Halfdata;)
    >
    > struct VertexDataStruct{
    > ...
    >
    > };
    >
    > struct EdgeDataStruct{
    > ...
    >
    > };
    >
    > struct TriangleDataStruct{
    > ...
    >
    > };



    ^^ oops im sorry there i repeated the same mistake (duplicate member)
    in half data but please ignore it as it is not signifcant to the
    discussion
     
    broli, Mar 26, 2008
    #8
  9. broli

    broli Guest

    I believe this is even a clearer approach as given in C FAQ 1.15 -

    struct VertexDataStruct;
    struct EdgeDataStruct;
    struct TriangleDataStruct;

    struct HalfDataStruct{
    struct HalfDataStruct *next;
    struct HalfDataStruct *previous;
    struct VertexDataStruct *origin;
    struct TriangleDataStruct *left;
    struct EdgeDataStruct *edge;
    };

    struct VertexDataStruct{
    struct HalfDataStruct *half;
    };

    struct EdgeDataStruct{
    struct HalfDataStruct *half;
    };

    struct TriangleDataStruct{
    struct HalfDataStruct *half;
    };

    typedef struct HalfDataStruct HalfData;
    typedef struct VertexDataStruct VertexData;
    typedef struct EdgeDataStruct EdgeData;
    typedef struct TriangleDataStruct TriangleData;
     
    broli, Mar 26, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kazack

    Question about Structures

    kazack, Nov 11, 2003, in forum: C++
    Replies:
    1
    Views:
    415
    Karl Heinz Buchegger
    Nov 11, 2003
  2. Saikrishna

    Data Structures Question...

    Saikrishna, Apr 6, 2004, in forum: C++
    Replies:
    3
    Views:
    471
    Michael Mendelsohn
    Apr 8, 2004
  3. Generic Usenet Account

    RTTI Question: typeid() for structures

    Generic Usenet Account, Feb 18, 2005, in forum: C++
    Replies:
    2
    Views:
    2,564
  4. tweak
    Replies:
    14
    Views:
    2,814
    Eric Sosman
    Jun 11, 2004
  5. Alfonso Morra
    Replies:
    11
    Views:
    740
    Emmanuel Delahaye
    Sep 24, 2005
Loading...

Share This Page