Thread: Silly Q about circular references

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Silly Q about circular references

    Hello,

    I have a silly question about circular references in C (note: not C++):

    typedef void ( *FSM ) ( CALL_CB* );

    typedef struct _fsm_cb
    {
    FSM fsm_entry;
    } FSM_CB;

    typedef struct _call_cb
    {
    FSM_CB *fsm;
    } CALL_CB;

    int main()
    {
    CALL_CB* call_cb;

    return 1;
    }
    This can't compile because FSM_CB is referencing CALL_CB and vice versa:
    test.c:4: syntax error before '*' token

    I know that C++ has a solution to this, by simply declaring the second class before the first:
    class Second;
    class First
    {
    Second* m_second;
    };
    class Second
    {
    First* m_first;
    };

    main ()
    {
    First* aFirst;
    Second* aSecond;
    }

    Can anyone help?

    Thanks,
    Paul

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I know that C++ has a solution to this, by simply declaring the second class before the first
    C has the same solution.
    My best code is written with the delete key.

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    struct _call_cb;
    
    typedef void ( *FSM ) ( struct _call_cb* );
    
    typedef struct _fsm_cb
    {
    
        FSM fsm_entry;
    
    } FSM_CB;
    
    typedef struct _call_cb
    {
    
        FSM_CB *fsm;
    
    } CALL_CB;
    
    int main()
    {
    
        CALL_CB* call_cb;
    
        return 1;
    
    }
    Possibly
    Last edited by SKeane; 10-04-2006 at 07:41 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    I had tried that, still doesn't work:

    struct _call_cb;

    typedef void ( *FSM ) ( CALL_CB* );

    ...

    test.c:3: syntax error before '*' token


    Any other ideas?
    Paul

  5. #5
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Try reading what I actually posted?

    I've highlighted the bit you missed.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Oops, thanks SKeane.

    That worked! Great!
    Paul

  7. #7
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Not saying it'll work mind!

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could also put the typedef beforehand (at least I think so, and Dinkumware does too):
    Code:
    typedef struct _call_cb CALL_CB;
    
    typedef void ( *FSM ) ( CALL_CB* );
    
    typedef struct _fsm_cb
    {
    
        FSM fsm_entry;
    
    } FSM_CB;
    
    struct _call_cb
    {
    
        FSM_CB *fsm;
    
    };
    
    int main()
    {
    
        CALL_CB* call_cb;
    
        return 1;
    
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes, that's valid. I would have posted it, but I was too lazy.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. best download site
    By gooddevil in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-20-2004, 10:36 PM
  3. Sreen Resolution Statistics?
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 04-26-2004, 02:33 PM
  4. declare references to references works!
    By ManuelH in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 08:14 AM
  5. circular shift
    By n00bcodezor in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 03:51 AM