Thread: inserting into linked list

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22

    inserting into linked list

    Alright, so I know that I'm supposed to use strcpy for strings, but I just can't get it to work. The program will just crash after entering the first bit of information. Here is the code I have for the insert function:


    Code:
    void insert(struct vehicle** head, char* make, char* model, int year, char* color, char* license)
    {
        struct vehicle* newvehicle;
        struct vehicle* crnt = (*head);
        
        newvehicle = malloc(sizeof(struct vehicle));
        newvehicle->make = make;
        newvehicle->model = model;
        newvehicle->year = year;
        newvehicle->color = color;
        newvehicle->license = license;
        
        
        
        
        if ((*head) == NULL || (*head)->license > license)
        {
            strcpy((*head),newvehicle->next);
            strcpy(newvehicle,(*head));
            return;
        }
        
        while(crnt->next != NULL && crnt->next->license < license)
        {
            strcpy(crnt->next, crnt);
        }
        
        strcpy(crnt->next,newvehicle->next);
        strcpy(newvehicle,crnt->next);
    }
    My intro teacher never went over strcpy or strcmp at all, so I really don't know how to use them.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, are you and vidioholic and SkidMarkz the same person or what? (If not, you should check their recent posts.)

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Orlando, Florida
    Posts
    22
    No, we're not the same person, but it does seem like we're in the same class. lol

    Even so, looking at theie code does not really help with my issues on inserting into a list...
    Last edited by MiroMage; 09-16-2008 at 10:18 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You would need to strcpy the license over. I don't know what you're trying to do with all the other strcpy's.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Remember to allocate memory for all of your strings before using strcpy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. Inserting new nodes in a sorted double linked list
    By carrja99 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2003, 08:34 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