Thread: Defining a struct with 2-way pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    74

    Defining a struct with 2-way pointers

    Hi there.

    I want to define a struct which has 2 pointers which point to diferent "things". I wanna have a linked list where each node is the head of a separate linked list. something like an excel sheet where you can dynamically resize the number of rows and columns, but with the exception that each "row" can have diferent number of columns

    Code:
     [floor1]->room101->NULL
        v
     [floor2]->NULL
        v
     [floor3]->room301->room302->room303->NULL
        v
     [floor4]->room401->room402->NULL
        v
      NULL
    i tried something like this but it doesn't work...

    Code:
    struct node1
    {
      int roomnumber;
      struct node1 *nextroom;
    }
    
    typedef struct node2
    {
      int floornumber;
      struct node1 room;
      struct node2 *nextfloor;
    } floor;

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I don't see why it doesn't work. Maybe you are not using it correctly? Post also your test code or the error you get.

    Note: Just want to point out that in C++ this could be done really easy, since the structures desired would be ready for you

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    74

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    struct node1
    {
      int roomnumber;
      struct node1 *nextroom;
    };  // Need a semicolon here!!!!!!
    
    typedef struct node2
    {
      int floornumber;
      struct node1 room;
      struct node2 *nextfloor;
    } floor;
    You forgot something there
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    stupid semicolon

    but tell me, I have other problems. how do I acess the field "roomnumber"?

    Code:
    floor *new2 = malloc(sizeof(floor));
    	new2->floornumber = 2;
    	new2->nextfloor = NULL;
    	new2->room->roomnumber = 201;
    	new2->room.roomnumber = 201;
    	new2.room->roomnumber = 201;
    the red are my suggestions. I must admit, I am really flaky when it comes to structures in structures which both have pointers

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by budala View Post
    how do I acess the field "roomnumber"?

    the red are my suggestions.
    The 2nd one is correct.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Why do you need struct node2 *nextfloor as a member of struct node2 because IMO it'll always point to NULL?

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by itCbitC View Post
    Why do you need struct node2 *nextfloor as a member of struct node2 because IMO it'll always point to NULL?
    It will point to the next node, since it is a linked list?

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    thanks guys. i get it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  3. Declaring a struct before defining it
    By hardi in forum C Programming
    Replies: 4
    Last Post: 12-07-2006, 01:51 PM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  5. My final data does not display
    By p1c1078 in forum C Programming
    Replies: 3
    Last Post: 04-23-2003, 06:32 AM