Thread: struct within a struct?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    39

    struct within a struct?

    For this program I have to write it seems I have to use a struct within a struct. I tried simply doing this:

    Code:
    typedef struct
    {
      ...
      typedef struct
      {
        ...
        ...
      } S2;
    } S1;
    I get an error when trying that. What is the correct way to do this? Thanks

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by BigFish21 View Post
    For this program I have to write it seems I have to use a struct within a struct. I tried simply doing this:

    Code:
    typedef struct
    {
      ...
      typedef struct
      {
        ...
        ...
      } S2;
    } S1;
    I get an error when trying that. What is the correct way to do this? Thanks
    Declare S2 before S1, not inside it. Then inside S1, create an instance of S2.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    struct S1
    {
      ...
      struct S2
      {
        ...
        ...
      } s2;
    } s1;
    
    or
    
    typedef struct
      {
        ...
        ...
      } S2;
    
    typedef struct
    {
      ...
      S2 s2;
    } S1;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Short answer:
    Code:
    typedef struct
    {
      ...
      struct  /* note: no typedef */
      {
        ...
        ...
      } S2;
    } S1;
    Sorry, I don't have time to explain this in much detail, but it boils down to typedefs defining a new type, while here you want to create part of another type. In short:
    Code:
    struct type {
        whatever contents;
    } instance;
    typedef struct type shortname;
    shortname is just a new name for "struct type". When you put a structure in another structure, you want to declare an instance of the structure (instance) rather than use struct type or shortname.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM