Thread: Structure inside Structure

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    7

    Structure inside Structure

    Hey guys

    I'm having trouble adding and reading a structure inside a structure. I tried double arrows, but that doesn't work.

    Code:
    NOT REAL CODE: 
    Struct 1{
       otherData  ;
       struct 2 ;
    } 
    
    struct 2{
        data;
    }
    To Add data: struct 2 I have struct1->struct2->data = something, but doesn't work.
    To read data: struct1->struct2->data but that doesn't work.

    (struct1->otherData works, but I guess it's because I haven't added the second struc data yet.. )

    I've never worked with structs inside structs before so I dont really know. Oh yeah my code is just as an example, not actual code.

    any input is highly appreciated.

    !! EDIT
    I have the pointer of struct 2 I want to add to struct 1.
    Last edited by tdep; 07-04-2007 at 06:01 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Use "." not "->"

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    wow I just read your reply, but I just figured it out too.

    My solution:

    struct1->struct2 = *ptr;

    to read:

    &(struct1->struct2)->data;

    I'm on a roll answering my stuff. Thanks for the reply though!!

    WOW 4 OF JULY HAS ME EXCITED. CANT WAIT FOR THE FIREWORKS

    WOOHOOO!!

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tdep View Post
    &(struct1->struct2)->data;
    That's sort of a pointless thing to do. &(x)->y means the same as x.y

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    
    struct FirstData
    {
          int Data;
          struct SecondData
          {
              int dAta;
          } Nested;
    };
    
    int main()
    {
        struct FirstData Node;
        struct FirstData * Node1;
        
        Node1 = malloc(sizeof(struct FirstData));
        Node.Data = 10;
        Node.Nested.dAta = 20;
        Node1->Nested.dAta = 30;
        Node1->Data = 40;
        
        printf("Nested Structures\n");
        printf("-----------------\n");
        printf("Node.Data               - &#37;d\n", Node.Data);
        printf("Node.Nested.dAta        - %d\n", Node.Nested.dAta);
        printf("Node1->Nested.dAta      - %d\n", Node1->Nested.dAta);
        printf("Node1->Data             - %d\n", Node1->Data);
        
        getchar();
        return 0;
    }
    
    /* My output 
    Nested Structures
    -----------------
    Node.Data               - 10
    Node.Nested.dAta        - 20
    Node1->Nested.dAta      - 30
    Node1->Data             - 40
    
    */
    Here is a good example for you

    ssharish2005
    Last edited by ssharish2005; 07-05-2007 at 04:51 AM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Here is a good example for you
    And where do you initialize Node1 pointer?
    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

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The joys of dealing with a compiler that protects you from yourself.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by vart View Post
    And where do you initialize Node1 pointer?

    That was a mistake sorry, I have corrected it now. Thanks for pointing it out.

    Thanks again.

    ssharish2005

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You also assume malloc succeeds and doesn't fail

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by zacs7 View Post
    You also assume malloc succeeds and doesn't fail
    Well, it was just an example, could have checked the return value.

    ssharish2005

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Still it's a good habit to keep, regardless of the code's purpose.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If you want to build a good example of the code - you should also add the free
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  2. passing pointer to a structure in a function
    By steve1_rm in forum C Programming
    Replies: 5
    Last Post: 02-03-2008, 02:48 AM
  3. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  4. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  5. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM