Thread: can't seem to print linked listed outside of local scope

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    can't seem to print linked listed outside of local scope

    i have a getString method which runs addElement and adds stuff to the linklist. Add element, finds the actual position in the linklist i.e. if its going at the start or middle etc so it runs newNode. My problem is
    when run my printList method within getString, the list is printed out fine. If i attempt to print it out in the main method a blank result is printed. For some reason the list contents are erased (i.e. the main method has no idea what happened). My code is below, i've kept it as minimal as possible:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct charNode
    {
       double numberone;
       double numbertwo;
       struct charNode *next;
    };
    
    typedef struct charNode charNode;
    
    void addElement(charNode **start, double numberone, double numbertwo);
    void printList(charNode *start);
    charNode* newNode(double numberone, double numbertwo);
    void getString(charNode *start);
    
    int main()
    {
       charNode *start = NULL;
    
       getString(start);
       printList(start); <-- does not.
       return 0;
    }
    
    
    void getString(charNode *start)
    {
    
         addElement(&start, 11.2, 33.3);
         addElement(&start, 12.3, 11.3);
         addElement(&start, 122.3, 11.3);
         printList(start); //<-- works
    }
    
    void addElement(charNode **start, double numberone, double numbertwo)
    {
    
       charNode *temp,*prev, *loc;
    
       temp = newNode(numberone, numbertwo);
    
    
       if (*start == NULL)
       {
          *start = temp;
       }
    
       else
       {
          loc = *start;
          prev = NULL;
    
          while (loc != NULL)
          {
             prev = loc;
             loc = loc->next;
          }
    
    
          temp->next = loc;
          if (prev == NULL)
          {
             *start = temp;
          }
          else
          {
             prev->next = temp;
          }
       }
    
    }
    
    
    charNode* newNode(double numberone, double numbertwo)
    {
    
       charNode *temp;
    
       temp = (charNode *) malloc(sizeof(charNode));
       if (temp == NULL)
       {
          printf("WARNING - Memory allocation error\n");
          exit(EXIT_FAILURE);
       }
    
    
       temp->numberone = numberone;
       temp->numbertwo = numbertwo;
    
    
       temp->next = NULL;
    
       return temp;
    }
    
    
    void printList(charNode *start)
    {
    
       while (start != NULL)
       {
    
          printf("%f %f\n", start->numberone, start->numbertwo);
          start = start->next;
       }
    }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Axel
    For some reason the list contents are erased (i.e. the main method has no idea what happened).
    See if this can give a clue:

    Code:
       printf("calling getstring(%p)\n", start);
       getString(start);
       printf("calling printList(%p)\n", start);
       printList(start); /*<-- does not. */
    D
    Last edited by Dave Evans; 10-21-2005 at 07:58 AM.

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Yes, that's what i've been trying to figure out. keeping a reference to the list.


    Here's where the actual reference is:

    Code:
    
    void getString(charNode *start)
    {
    
         addElement(&start, 1, 1);
         addElement(&start, 2, 2);
         addElement(&start, 3, 3);
         //printList(start);
    
    
    }
    so basically whatever is set in addElement to start it is saved to the local variable. I'm having trouble figuring out how to get that variable up to main. Since adding the elements is done in addElement.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Axel
    so basically whatever is set in addElement to start it is saved to the local variable. I'm having trouble figuring out how to get that variable up to main. Since adding the elements is done in addElement.

    Observations:

    You pass the variable start as an argument to getString.

    getString does some stuff and returns to main().

    The value of getString in main() is the same as it was before calling getString().


    Problem:

    You want getString's changes to start to be known in main().


    One possible solution:

    Change getString() and the call to getString() so that you can pass a pointer to start as an argument for getString().

    D

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    When you pass start into getString you are passing a copy of the pointer (a copy of the address that is already stored in start) to what is basically a new local pointer. Anything that happens with the addElement function affects the address stored in the local copy of the pointer and not the original pointer you passed into the function.

    There are a couple way to deal with this. The first is to have your function return a pointer to the modified local copy of the pointer passed into the function initially. This would get assigned back to your start pointer in main such that the call in main would look like start = getString(start);.

    The other way to deal with this is to have the getString function accept a pointer to pointer to charNode argument instead of a pointer to charNode as you have done with the addElement function. You would therefore call the function in main like so: getString(&start);. This would also affect how you call addElement within the getString function, i.e. addElement(start,...,...); instead of addElement(&start,...,...);.

    You got it all to work with the addElement function. I'm a little surprised you didn't do the same trick with the getString function itself.

    Note: The above repeats a bit of what Dave Evans said.
    Last edited by hk_mp5kpdw; 10-21-2005 at 10:16 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    but getString() does accept a pointer to start. I've tried assigning the value of start

    getString(&start);

    to the value of start in main but it doesn't work:

    24 assignment of pointer to pointer to struct charRecord to pointer to struct charRecord

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    temp = (charNode *) malloc(sizeof(charNode));
    You shouldn't cast malloc().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    start is null what i try :\

    Code:
    
    int main()
    {
       charNode *start = NULL;
    
    
       getString(&start);
       //start = getString(start);
       printf("calling printList(%p)\n", start);
       printList(start); /*<-- does not. */
       return 0;
    }
    
    
    charNode* getString(charNode **start)
    {
    
         addElement(&start, 1, 1);
         addElement(&start, 2, 2);
         addElement(&start, 3, 3);
         /*printList(start);*/
    
         return *start;
    
    
    }

  9. #9
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    As explained previously if you're using a pointer to pointer, you need to call addElement() like so: addElement(start,somenum,somenum);

    rather than &start, because addElement accepts a pointer to pointer, and that is what you have _NOW_ signified start as, in the getString function.

  10. #10
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    [QUOTE=Axel]

    You could do this
    Code:
    int main()
    {
       charNode *start = NULL;
    
       getString(&start);
       printf("calling printList(%p)\n", start);
       printList(start);    
       return 0;
    }
    
    
    void getString(charNode **start)
    {
    
         addElement(start, 1, 1);
         addElement(start, 2, 2);
         addElement(start, 3, 3);
    
    
    
    }
    Or this.
    Code:
    int main()
    {
       charNode *start = NULL;
       start = getString(start);
       printList( start );
       system( "pause" );
       return 0;
    }
    
    
    charNode* getString(charNode *start)
    {
         addElement(&start, 11.2, 33.3);
         addElement(&start, 12.3, 11.3);
         addElement(&start, 122.3, 11.3);
         return start;
    }

  11. #11
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ok right, i was confused with **start. Your first approach doesn't work btw (null reference)

  12. #12
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Im sure both would work. You probably didnt change your function prototype. From

    charNode* getString(charNode ** )

    to

    void getString(charNode **start)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  2. I need help~~Hash~~THX!
    By freefallin in forum C Programming
    Replies: 5
    Last Post: 04-22-2004, 07:50 AM
  3. linked lists problem
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-17-2002, 10:55 AM
  4. linked list
    By Flex in forum C Programming
    Replies: 1
    Last Post: 02-19-2002, 10:32 AM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM