Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C linked list with an array inside

  1. Oct 17, 2013 #1
    I have pasted my code here : http://pastebin.com/nMNarH9i
    It is in language C.
    Basically, I am reading in 5 numbers at a time.
    Putting these numbers in array one_vote_array.
    Then I need to put this array in a structure vote_node.
    I also then change my next pointer in the structure to head and then make head point to this new structure.
    I want to to keep on making structures while I keep reading in 5 numbers.
    The problem is my program is only printing the last number i inputted in it.
     
  2. jcsd
  3. Oct 17, 2013 #2

    jedishrfu

    Staff: Mentor

    Clearly a case of voter fraud.

    so the way your program is written it will print the voting backward right? last voter to first voter.

    but in your case it prints the last voter and stops so that implies that the temp->next is null.

    Id go back to the first for loop and use your print test to see what voter->node is beiing set to as you iterate thru your voters.

    Do you have a debugger that you can run your program thru?

    it would allow you to review the values of variables step by step.
     
  4. Oct 17, 2013 #3

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    You are reusing the same array of five integers for each node. You need to allocate a new array of five integers for each node.
     
  5. Oct 17, 2013 #4

    jedishrfu

    Staff: Mentor

    Good catch, but I thought we were supposed to lead the OP to the answer.
     
  6. Oct 17, 2013 #5
    But my function read_one_vote changes the contents of the array one_vote_array everytime it is called, so shouldn't that array be different for each node?
     
  7. Oct 17, 2013 #6

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    How can it be different for each node? Your code makes exactly the opposite be true. It's the same array for each node.

    If you need a different array for each node, you need to make a different array for each node.
     
  8. Oct 17, 2013 #7
    Ok, I did what you said, and it works now. Thank you very much.
     
  9. Oct 17, 2013 #8
    I still am finding it confusing though, can't I use the same array with different value and set it to each node?
     
  10. Oct 17, 2013 #9
    Because the values of one_vote_array change each time the function read_one_vote gets called
     
  11. Oct 17, 2013 #10
    Think about it at the hardware level: If you need to keep track of say, 9 different arrays, how many arrays would you need to allocate in memory? Each block of memory can only hold one value at a time, so in order for your computer to "remember" what the content of each array is, it needs to allocate a separate memory block for each one.

    What your asking is possible though. If you want, you can have a buffer in which you construct each array you want. Then when it is ready, you can copy it to another newly allocated array, and reset the original array. However, this still requires you to allocate a new array each time.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C linked list with an array inside
  1. Linked List (Replies: 9)

  2. Linked list (Replies: 12)

Loading...