list error

This is a discussion on list error within the C Programming forums, part of the General Programming Boards category; i have just tried to implement a simple linked list to store a name such as "ben", however it compiles ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    67

    list error

    i have just tried to implement a simple linked list to store a name such as "ben", however it compiles but doesnt print out the values, here is my code:

    Code:
    #include <stdio.h>
    
    typedef char DATA;
    
    struct linked_list{
        DATA d;
        struct linked_list *next;
    };
    
    typedef struct linked_list ELEMENT;
    typedef ELEMENT* LINK;
    
    LINK string_to_list(char s[])
    {
        LINK head;
        
            if( s[0]=='\0')
            return NULL;
        else{
            head=malloc(sizeof(ELEMENT));
            head->d=s[0];
            head->next=string_to_list(s+1);
            return head;
        }
    }
    
    int main(void)
    {
        char *s;
        LINK head=NULL;
        LINK tail=NULL;
        LINK next;
        char a;
        scanf("%s", string_to_list(&s));
         for( ; head ; head = next ) {
        printf( "%c", head->d );
        next = head->next;
        free( head );
        }
        return 0;
    }
    where have i gone wrong?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > scanf("%s", string_to_list(&s));
    I'm pretty sure this doesn't compile - your compiler is broken if it does.
    1. read input into a separate string
    2. assign the return result of this function to head

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    it does compile, but with warnings... then it crashes

    here is my code now:

    Code:
     #include <stdio.h>
    
    typedef char DATA;
    
    struct linked_list{
        DATA d;
        struct linked_list *next;
    };
    
    typedef struct linked_list ELEMENT;
    typedef ELEMENT* LINK;
    
    LINK string_to_list(char s[])
    {
        LINK head;
        
            if( s[0]=='\0')
            return NULL;
        else{
            head=malloc(sizeof(ELEMENT));
            head->d=s[0];
            head->next=string_to_list(s+1);
            return head;
        }
    }
    
    int main(void)
    {
        
    	char *b;
        LINK head=NULL;
        LINK tail=NULL;
        LINK next;
         scanf("%s", b);
    	string_to_list(b);
         for( ; head ; head = next ) {
        printf( "%c", head->d );
        next = head->next;
        free( head );
        }
        return head;
    }
    are there any tutorials for more advanced list topics such as reversing them and deleting elements in a list? i can only seem to find simple ones on the concept of lists.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Are you even paying the slightest bit of attention?

    > char *b;
    Where does this point?
    Answer - nowhere, so where do you think scanf("%s", b); is going to write all your input?

    > string_to_list(b);
    I said "2. assign the return result of this function to head"
    How far do you think your for loop will get without this assignment?

    > return head;
    main returns an int

    > it does compile, but with warnings
    I should hope so - perhaps you should focus on addressing those before trying to run the code.
    Newbies have no business running code which contains warnings.


    Oh, and include stdlib.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  2. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

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