Thread: Link list - what is wrong

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    2

    Post Link list - what is wrong

    Hi,
    I am trying to add item to a link list i am having trouble the head is always null, Please help!!!!
    i am using the list in several functions
    this is the link list defintion:
    Code:
    typedef struct LinkList *ptr_fl;
    
    
    typedef struct    LinkList {
        int linenumber;
        ptr_fl next;
    } Item;
    
    int main(int argc, char *argv[])
    {
       ptr_fl *head=NULL;
       beforeInsert(&head);
      --here the head is null
    }
    
    void beforeinsert(ptr_fl *head)
    {
          add2list(head);
    }
    
    
    void add2list(ptr_fl *head)
    {
         ptr_fl p1 = *head;
    
        ptr_fl new_line = (ptr_fl)malloc(sizeof(FileLine));
        new_line->linenumber = linenumber;
        new_line->next = NULL;
    
    
        if (p1 == NULL)
        {
            p1 = new_line;
        }
        else
        {
    
    
            while (p1->next != NULL)
                p1 = p1->next;
    
    
            p1->next = new_line;
        }
        return;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > p1 = new_line;
    You need to do
    *head = new_line;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Code:
    typedef struct LinkList *ptr_fl;
    You hide a pointer behind a typedef.
    Code:
    …
       ptr_fl *head=NULL;
    …
    'head' is now defined as a pointer to ptr_fl, which is also a pointer (hidden in typedef).
    So, 'head' is now a double pointer to 'struct LinkList'.
    Code:
    …
       beforeInsert(&head);
    …
    You give the address of 'head' as argument. The argument is now a triple pointer to 'struct LinkList'.
    But if we look at the function 'beforeinsert':
    Code:
    void beforeinsert(ptr_fl *head)
    {
        …
    This function wants a pointer to 'ptr_fl' (which is also a pointer to 'struct LinkList').
    In real, the function wants a double pointer to 'struct LinkList'.
    But you give a triple pointer.


    Conclusion:
    Don't hide a pointer behind a typedef!
    You will confuse other programmers and in the end you confuse yourself!
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-26-2013, 10:52 AM
  2. link list
    By dyn4sty22 in forum C Programming
    Replies: 5
    Last Post: 04-17-2008, 07:07 PM
  3. help on link list
    By bg1906 in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2007, 06:06 PM
  4. Left justification using linked list of link list
    By hykyit in forum C Programming
    Replies: 7
    Last Post: 08-29-2005, 10:04 PM
  5. what is wrong is this link list?
    By dc_128 in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2003, 03:30 PM

Tags for this Thread