C struct : problem with structs containing each other

Sharky Forums


Results 1 to 4 of 4

Thread: C struct : problem with structs containing each other

  1. #1
    Sushi
    Join Date
    Jul 2005
    Posts
    3

    C struct : problem with structs containing each other

    Hi

    I'm having problems getting my structs to compile. I tried to find a solution from my K&R C book and the web, but couldn't. It's really annoying, because it's probably just some syntax problem, but I just can't figure it out.

    Basically I'm trying to have two structs that both contain member whose type is the other struct.

    Here's the problem ( simplified from my code ):

    a.h:

    Code:
    #ifndef _A_H_
    #define _A_H_
    
    #include"b.h"
    
    typedef struct{
        b_struct bs;
    } a_struct;
    
    #endif
    b.h:

    Code:
    #ifndef _B_H_
    #define _B_H_
    
    #include"a.h"
    
    typedef struct{
        a_struct as;
    } b_struct;
    
    #endif
    c.c:
    Code:
    #include"a.h"
    When I try to compile, I get:

    C:\Program Files\eclipse\workspace\testi>gcc c.c
    In file included from a.h:4,
    from c.c:1:
    b.h:7: error: parse error before "a_struct"
    b.h:7: warning: no semicolon at end of struct or union
    b.h:8: warning: data definition has no type or storage class
    In file included from c.c:1:
    a.h:7: error: parse error before "b_struct"
    a.h:7: warning: no semicolon at end of struct or union
    a.h:8: warning: data definition has no type or storage class

    What's wrong and how to fix it?

    Thanks for reading and for any advice/pointers in advance.

  2. #2
    Sushi
    Join Date
    Jul 2005
    Posts
    3
    I got some advice, and found out, that the problem was basically that I didn't use pointers. Because I didn't use pointers, compiler couldn't figure out the size of structs a and b and complained. With pointers it works, because the size of all pointers is the same and known to compiler.

    Here's the solution I ended up with:
    Code:
    //A.h
    #ifndef _A_H_
    #define _A_H_
    
    #include"b.h"
    
    typedef struct _a_struct{
    	struct _b_struct *bs;
    } a_struct;
    
    #endif /*_A_H_*/
    
    //B.h
    #ifndef _B_H_
    #define _B_H_
    
    #include"a.h"
    
    typedef struct _b_struct{
    	struct _a_struct *as;
    } b_struct;
    
    #endif /*_B_H_*/
    
    //C.c
    #include"a.h"
    Compiles and works. Finally.

  3. #3
    Mako Shark slavik's Avatar
    Join Date
    May 2001
    Location
    Brooklyn
    Posts
    3,308
    why typedef them? in the working example, you don't need the typedefs ...
    Activation? What activation?
    Quote Originally Posted by Geekkit (from ubuntu forums regarding 'goto' statement)
    Yep it sure does. So does crack cocaine. Existence is not a valid endorsement for being acceptable.
    Quote Originally Posted by Linus Torvalds
    Only wimps use tape backup: _real_ men just upload their important stuff on ftp, and let the rest of the world mirror it

  4. #4
    Sushi
    Join Date
    Jul 2005
    Posts
    3
    Typedefs were used int the example, because I used them in my original code from which the example was simplified and they were part of the problem. In my original code, I used typedefs to make a struct that was used a lot easier to use and to make code easier to read.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •