struct within a struct?

This is a discussion on struct within a struct? within the C Programming forums, part of the General Programming Boards category; For this program I have to write it seems I have to use a struct within a struct. I tried ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,237
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    struct S1
    {
      ...
      struct S2
      {
        ...
        ...
      } s2;
    } s1;
    
    or
    
    typedef struct
      {
        ...
        ...
      } S2;
    
    typedef struct
    {
      ...
      S2 s2;
    } S1;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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, 02: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, 08:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21