nested structures and pointers

This is a discussion on nested structures and pointers within the C Programming forums, part of the General Programming Boards category; I have a conceptual question. If I have some structures declared thus: Code: struct film { char title[TSIZE]; int rating; ...

  1. #1
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    75

    nested structures and pointers

    I have a conceptual question. If I have some structures declared thus:

    Code:
    struct film
    {
       char title[TSIZE];
       int rating;
    };
    
    typedef struct film Item;
    
    typedef struct node
    {
       Item item;
       struct node * next;
    }  Node;
    
    typedef Node * List;
    And I decide to re-define List as:

    Code:
    // typedef Node * List;
    
    typedef struct list {
       Node * head;
       Node * end;
    } List;
    Would the references to the item member of Node then become:

    Code:
    List * plist;
    
    plist->head->item;
    // and/or
    plist->end->item;
    I ask because I had a structure assignment that used to work:

    Code:
    static void CopyToNode(Item item, Node * pnode)
    {
       pnode->item = item;  // copying structure
    }
    After the 'List' definition change (mentioned above), I changed the CopyToNode() to:

    Code:
    static void CopyToNode(Item item, List * plist)
    {
       plist->end->item = item;  // copying structure
    }
    The files compile with no errors, but the program crashes on the 'copying structure' line.

    What am I forgetting?
    Steering Wheel: GCC in Code::Blocks 10.05
    Batmobile: Linux Mint 14, running Linux 3.5 and KDE 4.9.2
    Rusty Old Trailer For Hauling Games: Windows XP SP3
    Engine: AMD FX-8350 Vishera 8-core
    Dashboard: NIVIDIA GTX 570 HD

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Is plist pointing to valid memory location?
    If so, how about plist->end?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 03:44 AM
  2. Help with nested struct pointers
    By vampireiam in forum C Programming
    Replies: 10
    Last Post: 11-13-2007, 10:59 AM
  3. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 05:31 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 11:53 AM

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