Defining a struct with 2-way pointers

This is a discussion on Defining a struct with 2-way pointers within the C Programming forums, part of the General Programming Boards category; Hi there. I want to define a struct which has 2 pointers which point to diferent "things". I wanna have ...

  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,806
    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,057
    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
    Oct 2008
    Location
    TX
    Posts
    2,057
    Yup, that's why setting it to NULL didn't make sense in the code snippet posted by the o/p unless that's the top floor

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    Quote Originally Posted by C_ntua View Post
    It will point to the next node, since it is a linked list?
    yes it would point to a next node if there were one

    Quote Originally Posted by itCbitC View Post
    Yup, that's why setting it to NULL didn't make sense in the code snippet posted by the o/p unless that's the top floor
    you're right. i chose to have just the top floor.

    Quote Originally Posted by MK27 View Post
    The 2nd one is correct.
    thanks. so what about the pointer nextroom? which is it

    Code:
    new2->room.nextroom = newroom;
    new2->room->nextroom = newroom;

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by budala View Post

    thanks. so what about the pointer nextroom? which is it

    Code:
    new2->room.nextroom = newroom;
    new2->room->nextroom = newroom;
    How could that possibly be different from before? The type of the thing you are trying to access is completely irrelevant to whether you use -> or . -- only whether you are trying to access that object from a thing pointed to (where you use ->) or a thing itself (where you use .).

  12. #12
    Registered User
    Join Date
    Mar 2009
    Location
    China
    Posts
    2
    Code:
    typedef struct node2
    {
      int floornumber;
      struct node1 *room;  // use pointer to room header
      struct node2 *nextfloor;
    } floor;
    Last edited by wj.z; 09-11-2009 at 02:06 AM.

  13. #13
    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, 07:32 AM

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