Thread: Structure/Linked list pointer Crash

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    3

    Structure/Linked list pointer Crash

    After creating a structure and trying to assign the address of a node to the linked list, the program builds correctly, however crashes upon running. Any help? Thanks.

    Code:
    struct student{
        struct student* back;
        char first[size];
        char last[size];
        double score;
        int zip;
        struct student* next;
    };
    struct student *head;
    struct student *tail;
    struct student *current;
    struct student *temp;
    
    int main()
    {
        head = NULL;
        tail = NULL;
        tail->back = head;
        head->next = tail;
        current = head;
    }
    This issue seems to be due to the lines of code "tail->back = head;" and "head->next = tail;".

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    "tail" and "head" are NULL, so where in memory do you think "tail->back" and "head->next" are?

    Answer: Nowhere. You can't dereference a NULL pointer. You need to allocate memory for them before you are able to use their members.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 09-18-2015, 02:35 PM
  2. linked list of structure
    By johnhuge in forum C Programming
    Replies: 7
    Last Post: 09-19-2011, 10:42 PM
  3. structure and a linked list help
    By mikecool291 in forum C Programming
    Replies: 1
    Last Post: 05-10-2009, 10:39 AM
  4. Replies: 1
    Last Post: 04-02-2009, 06:51 AM
  5. Linked list w/ structure
    By 5rjK in forum C Programming
    Replies: 10
    Last Post: 12-12-2006, 06:18 AM

Tags for this Thread