linked list with char array HELP!

This is a discussion on linked list with char array HELP! within the C++ Programming forums, part of the General Programming Boards category; i'm writing a basic linked list program, prompting to enter (among a lot of things) a description. everything is coming ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    linked list with char array HELP!

    i'm writing a basic linked list program, prompting to enter (among a lot of things) a description. everything is coming out right but somehow the description is getting lost before it can be outputted. i know i'm making a basic error here, so i'm hoping someone can tell me what i'm doing wrong. here's my code, edited for the description part:

    Code:
    struct
    {
        char descrip[25];
        node* next;
    };
    
    void InsertDescrip(char [] );
    node* head=NULL;
    
    int main()
    {
    char des[25];
    node* current=NULL;
         cout<<"Enter description:  "<<endl;
         cin>>des;
    
         current= new node;
         InsertDescrip(des);
         head=current;
    
         cout<<"Press any key to terminate"<<endl;
         getch();
    }
    
    void InsertDescrip(char des[25] )
    {
        current->descrip[25]=des[25];
        current->next=head;
    }
    Last edited by littlepiggy; 10-18-2005 at 02:43 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    You should use the C++ string class to handle strings. It has natural assignment semantics.

    If you want to assign a character array, you should use strcpy.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    thanks daved. i got it to work using the string class, but unfortunately i have to use a char array. i had a feeling that strcpy was probably what i was going to have to use, but as i haven't used strcpy's in a while, i was thinking that perhaps it was something a little more than that.... i'll play around with it and see how it goes.... thanks again!

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    well, first you aren't passing your "current" variable from main to InsertDescrip, so it probably doesn't compile. second, you are assigning only the char at position 25 from the passed in description to the char at position 25 of your descrip variable, since the char array only has 25 elements, position 25 is invalid (it must be 0 to 24), like Daved said, use strcpy for this instead. Third, you are assigning the next variable to be head, that doesn't seem to make sense to me. Oh, I guess you are inserting at the head and treating it like a stack or something instead of a normal linked list where you insert at the back.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    thanks for your input rocky. i had some serious typos at the end of my post there. i went back and fixed those. i'm going to try and make some progress with strcpy like you guys suggest. thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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