Thread: simple list question

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    simple list question

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct List {char array[10]; struct List *next;} List;
    
    List *insert_list (char head[10], List *tail)
    {
        List *t = calloc (1, sizeof (List));
        strcpy (t -> array, head);
        t -> next = tail;
        return t;
    }
    
    int print (List *t)
    {
        while (t != NULL){
            printf ("%s", t -> array);
            t = t -> next;
        }
        printf ("\n");
        return 0;
    }
    
    int main (void)
    {
        int i;
        char c[10];
        List *l = NULL;
        while (strcmp ('.', c) != 0){
            scanf ("%s", c);
            l = insert_list (c, l);
        }
        print (l);
        return 0;
    }
    I can't figure out why the code wont work. I keep getting "bus error" message. All I am trying to do is make the program read in words until i press '.' after that i want to print those words. Where am I going wrong? thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because you are comparing the input string with the location 46 in memory, perhaps?

    Use double-quotes when forming a string, single quotes is a char on it's own, and when used as a pointer, usually invalid.

    Also, enable warnings when you compile, and the compiler (at least most compilers) will inform you about incorrect use of parameters to for example strcmp().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    Thanks a lot, now it all works. Just shows that it's not always good idea to follow the guidelines your lecturer gives you, since obviously his code is blatantly wrong lol.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by agentsmith View Post
    Thanks a lot, now it all works. Just shows that it's not always good idea to follow the guidelines your lecturer gives you, since obviously his code is blatantly wrong lol.
    That seems to be a recurring theme among threads posted on this board.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. simple list and driver program help
    By rugger78 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 03:37 AM
  4. Simple list code
    By JimpsEd in forum C Programming
    Replies: 1
    Last Post: 05-24-2006, 02:01 PM
  5. linked list stack question
    By Drew in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2003, 05:05 AM