Thread: need help building a linked list

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    need help building a linked list

    i'm trying to build a linked list from an input txt file.

    example input file (note: the actual file has more parameters):

    NAME: John
    NUMBER: 3

    NAME: Fred
    NUMBER: 4

    etc...

    i wrote a bunch of code, but i kept getting bus error. i edited the heck out of it and settled for trying to just get the name to read, and i still got a bus error.

    any ideas?

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    typedef struct people_node{
        char *first_name;
    }people;
    
    
    int main(int argc,char *argv[])
    {
        FILE *community;
        people *current;
        char line[100], *token;
    
        community = fopen(argv[1], "r");
        current = malloc(sizeof(people));
        while(fgets(line,100,community)!=NULL){
            token = strtok(line," ");
            token = strtok(NULL,"\n");
            strcpy(current->first_name, token);
        }
        printf("%s\n", current->first_name);
        
        return 0;
    }
    the argc, argv thing is because i have another input file for other purposes...

  2. #2
    Registered User
    Join Date
    Dec 2009
    Location
    /dev/pts/0
    Posts
    29
    Technically, a linked list should probably have at least a next pointer, so like . . .

    Code:
    struct linked_list_node_t {
        /* data elements . . . */
        struct linked_list_node_t *next;
    }
    Does that make sense? Otherwise, you're just using a straight structure to store data.

    NOTE: I may be misunderstanding you here. I apologize if I am.
    -- strange

    There is no Darkness in Eternity
    Only Light too dim for us to see

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    What you're doing is the equivalent of this:
    Code:
    char *first_name;
    strcpy(first_name, "bob");
    See the problem? You're not allocating any memory for the string, so you wind up copying to some random address.

    “Ah hah,” you say, “but I used malloc().”

    Correct. But you only allocated memory for the struct. One part of the struct is a pointer, so you allocated memory for the pointer; but not what the pointer points to. You either need to allocate memory for current->first_name, or you could make current->first_name an array of char. For now an array of 100 bytes would work fine because your fgets() will never read more than that; but do be aware of problems with writing more to an array (or, for that matter, malloc()'d memory) than you have space for.

    When you get a bus error (or something similar like a segfault), your first step should be to use a debugger. I recommend Valgrind if it supports your platform. gdb is useful, too.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    so like this?

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    typedef struct people_node{
        char *first_name;
    }people;
    
    
    int main(int argc,char *argv[])
    {
        FILE *community;
        people *current;
        char line[100], *token;
    
        community = fopen(argv[1], "r");
        current = malloc(sizeof(people));
        while(fgets(line,100,community)!=NULL){
            token = strtok(line," ");
            token = strtok(NULL,"\n");
    
            current->first_name = (char *)malloc((strlen(token)+1)*sizeof(char)); 
    
            strcpy(current->first_name, token);
        }
        printf("%s\n", current->first_name);
        
        return 0;
    }
    i had something like this in another iteration of the code, and i still got a bus error. i am using code::blocks right now, but i don't know for the life of me how to debug with it, but that's for another time.

    P.S. yeah the thread name is a little misleading. sorry about that. during my edits, i forgot that i had taken the pointer out. eventually i want include a linked list to link entries together.
    Last edited by supern00b; 12-10-2009 at 11:17 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Better. You keep allocating new memory in your loop, but you're not keeping track of the old memory, so you're ending up with a memory leak. Plus you never free anything at the end either.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    so if i put the linked list part back in, like fill in first_name for one link, then go to the next one, would that fix it? then free the space after everything else?

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by sufoode View Post
    #include <list>
    That's absurd! For one this is the C forum, not the C++ one, and secondly it is obvious that this is some kind of learning exercize.

    supern00b: You would be better off using some linked-list tutorial at first. There are plenty of them around. Cprogramming.com Tutorial: Linked Lists
    If you get stuck while going through that, then ask your questions here.
    Otherwise we'd only be duplicating here what is already written down.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sufoode
    Why don't you upgrade from that archaic language?
    Stop trolling. This is the C programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM