Thread: Defining a struct with 2-way pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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

  3. #3
    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

  4. #4
    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?

  5. #5
    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?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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

  7. #7
    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;

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