Linked Lists with chars.

This is a discussion on Linked Lists with chars. within the C Programming forums, part of the General Programming Boards category; This has been driving me crazy for the past few hours. I'm attempting to take in two char pointers and ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Linked Lists with chars.

    This has been driving me crazy for the past few hours. I'm attempting to take in two char pointers and a float as parameters. I then take the first 10 letters of the first char pointer and assign them as a char array to part of my linked list. I then do the same with the next char, however, if the first char is over 10 letters, the second is added to the first char.
    So, if I enter me 234234234, the first char arrays are "me" and the second is "234234234".
    If I enter morethantenletters 234234234, the first char is "morethantenletters234234234" and the second is "234234234". I have no idea why that gets concatenated with the first. With print statements I found out that the first char array gets screwed up after the second char array is put in the linked list. Anyway, heres the code:
    Code:
    struct node{
    	      char name[10];
    	      char acctno[9];
    	      float balance;
    	      struct node *next;
    	} node;
    Code:
    int BANKLOGadd(const char* name, const char* acctno, float balance) { 
      int i = 0;
      struct node * newNode = malloc(sizeof(struct node));
      for (i=0; i<10; i++){
          newNode->name[i]=name[i];
      }
    
    /*always prints first char correctly*/  
    printf("%s\n", newNode->name); 
      for (i=0; i<9; i++){
        newNode->acctno[i]=acctno[i];
      }
    
    /*always prints first char correctly unless it's over 10*/ 
      printf("%s\n", newNode->name);
      newNode->balance =  balance;
      newNode->next= head;
      head = newNode;
      printf("ADD %s %s %f\n", name, acctno, balance);
      return 0; 
    }
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Firstly why you want to make you life harder?
    In the loop where you pass each letter to the other, use strcpy instead. Also check for lenght errors, because the input string length can be greater than ten characters. Return an error there or just copy ten exact characters, well there will be nine(9) as the tenth character is always used to store the '\0' for a C string. If you try to insert a bigger string in a smaller array the result will be undefined. Overflow is your problem.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    Thanks, I see what I did wrong. I changed the 10 to an 11 and now it works. I can't believe I wasted all that time on such a stupid little mistake. Anyway, I'm using the loops since I was having trouble with passing the char array to strncpy. The arrays ended up blank.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 05:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 09:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 07:25 PM

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