linked list and pointers : ??????

This is a discussion on linked list and pointers : ?????? within the C Programming forums, part of the General Programming Boards category; Hi ! This is a program I wrote which compiles fine but gives me and error on running it .. ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    linked list and pointers : ??????

    Hi !

    This is a program I wrote which compiles fine but gives me and error on running it .. I need some help on finding what I am doing wrong in this ????



    #include<stdio.h>
    #include<stdlib.h>


    Code:
    /*  ********************************************************** */
    /*  This program creates a linked  list */
    /*  ********************************************************** */
    
    typedef struct node* link;
    
    struct node
    {
           int item; 
           link next;
    };
    void makelist(link x, int y);
    void printlist(link x);
    
    int main()
    {
        link x;
        makelist(x, 5);
        printlist(x);
        return 0;
    }
    
    
    void makelist(link x, int num)
    {
         int i;
         if(0 == num)
         {
             x = NULL;
             return;
         }
         link a = malloc(sizeof *a);
         x = a;
         a->item = 1;
         if(1 == num)
         {
              a->next = NULL;
              return;
         }
         for(i = 2; i <= num; i++)
         {
             a->next = malloc(sizeof *a);
             a = a->next;
             a->item = i;
             if(i == num)
              {
                   a->next = NULL;
                   break;
              }
         }
    }
    
    void printlist(link x)
    {
        link b = x;
        while(b != NULL)
        {
             printf("%d, ", b->item);
             b = b->next;   
        }
    }

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Probably a seg fault error right? Wow I must be a psychic. Have you tried running a debugger? Try and narrow down what function or even what line the error is occurring on.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Code:
    link a = malloc(sizeof *a);
         x = a;
    This won't work, if you wanted to change the "link x" in the main function you need to pass a pointer-to-pointer for your insert function. If you don't want to do that you can make the return type of the insertion function return the head node instead of being a void.
    The cost of software maintenance increases with the square of the programmer's creativity.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    yeah I changed the function to " link makelist(link x, int num)" and it worked but I don't understand why do I need to do that ?

    can you please tell why do I need to do that ??
    can you give me a function declaration to the second option of a pointer to pointer ??

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    It's to do with local variables, whenever you call a function - the function will make "copies" of the parameters you send in on the stack. So the link x you modify will not be the same as the link x in the main function, which is why you need to send a pointer back to link x in the main to point to the right thing after the insertion function modified it.

    A pointer-to-pointer prototype is like so:
    Code:
    void makelist(link* x, int num);
    The cost of software maintenance increases with the square of the programmer's creativity.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    but link x is a pointer to a structure itself ???

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Which is a pointer to "pointer to a structure". Hence pointer to a pointer.
    The cost of software maintenance increases with the square of the programmer's creativity.

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    good stuff, thanks a lot !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 02:29 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM

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