Thread: Problems with Lists

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    3

    Problems with Lists

    So I'm reading a file line by line and storing it backwards into a list. So if the file has has this format...
    1
    2
    3
    4
    The code should store each line in a list as such...
    4, 3, 2 ,1

    Instead the code will store the last variable in all nodes. So the final list will look like this...
    4, 4, 4, 4

    Here is my code...
    Code:
    struct node *head = NULL;
    
    int i;
    
    while(read(in, &i, sizeof(int)) != 0) {
        struct node *temp = malloc(sizeof(*temp));
        temp->line = &i;
        temp->next = head;
        head = temp;
    }
    Anybody have any ideas?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're not reading the file "line by line" but "binary integer by binary integer".
    If you actually want to read it line by line you need to do it differently.
    Is it an ascii file that you can view with "cat" (on unix) or "notepad" (on windows)?

    As for what's occurring in the posted snippet:
    * Using the ampersand when saving i saves it's address, not it's value.
    * Since the address of i is unchanging, you save the same address in every list node.
    * Since the last value that i contains is 4, you end up printing all 4's.

    How to fix it depends on what you're actually doing which we can only know after you answer the questions above.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    Quote Originally Posted by oogabooga View Post
    You're not reading the file "line by line" but "binary integer by binary integer".
    If you actually want to read it line by line you need to do it differently.
    Is it an ascii file that you can view with "cat" (on unix) or "notepad" (on windows)?

    As for what's occurring in the posted snippet:
    * Using the ampersand when saving i saves it's address, not it's value.
    * Since the address of i is unchanging, you save the same address in every list node.
    * Since the last value that i contains is 4, you end up printing all 4's.

    How to fix it depends on what you're actually doing which we can only know after you answer the questions above.
    No it is not an ascii file. If you run "cat" on this file you will just get a bunch of random characters.

    I added a "printf" that prints what is being put into the list durring each loop. This is the output I get.

    loop one:
    1

    loop two:
    2 2

    loop three:
    3 3 3

    loop four:
    4 4 4 4

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That's exactly what you would expect to get given my previous explanation.

    So you need to save the value of i, not it's address.
    Remove the ampersand from temp->line = &i;


    You haven't posted the rest of your code so there could be other problems.
    In particular, temp->line should be an int (not an int*).
    And you need to print it like printf("%d ", temp->line)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    Thanks for you help oogabooga. You were right about the "&" I should have known better. It is working fine now. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very problems with Lists
    By Yannick in forum C Programming
    Replies: 1
    Last Post: 04-29-2008, 08:36 AM
  2. problems with structures and linked lists
    By jwillisoa in forum C Programming
    Replies: 7
    Last Post: 07-01-2007, 05:23 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. [Linked Lists] Custom String Class Add Char problems
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 07-23-2004, 10:15 PM
  5. Problems with linked lists from *.dat
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-03-2002, 05:22 PM