Double pointers and linked lists.

This is a discussion on Double pointers and linked lists. within the C Programming forums, part of the General Programming Boards category; I'm creating an a* algorithm implementation using two double linked lists. p_node is the node being copied. ppDest is the ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Double pointers and linked lists.

    I'm creating an a* algorithm implementation using two double linked lists.

    p_node is the node being copied. ppDest is the tail node and ppDestlist_h is the head node.

    I'm not entirely sure about my double pointers and I might just be making a schoolboy error here. I only learnt about their use yesterday, and have converted my code to use it instead of returning pointers to the nodes.

    Code:
    int addtolist(p_node *node, p_node **ppDest, p_node *ppDestlist_h)
    { 
    
      if(( node == NULL ))
      {
           printf("Node pointer is null!\n");
           return 1;
      }
    
      /*edit* /
      ppDest = &(*ppDestlist_h);
      
      /*double pointers here are used to keep going forward until null is hit*/
      /* if null is hit immediately then copy the node right away*/
      while(*ppDest != NULL)
       { ppDest = &(*ppDest)->next;}
       
       memcpy(&(*ppDest), &node, sizeof(p_node));
       
       printf("node inside func: %d Current:  \n", (*ppDest)->x); 
      
      /*now I set the next value of the node to NULL*/
      
      (*ppDest)->next = NULL;
      
      return;
      
    }
    This seems like it should work, but doesnt want to.

    has anyone got any pointers? (ouch that was bad)

    thanks,

    -Simo
    Last edited by simo_r; 05-05-2008 at 10:26 AM. Reason: clarification

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    It took me some time to read this horrible code...

    Code:
    memcpy(&(*ppDest), &node, sizeof(p_node));
    &(*ppDest) is just ppDest - pointer to pointer
    &node - is pointer to pointer as well
    so
    *ppDest = node; should be enough...

    instead of it you are trying to copy other the pointer the whole p_node structure of bytes (from location that does not contains enogh info) overwriting all memory after ppDest ...

    It does not seems as it should work...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    2
    Quote Originally Posted by vart View Post
    It took me some time to read this horrible code...

    Code:
    memcpy(&(*ppDest), &node, sizeof(p_node));
    &(*ppDest) is just ppDest - pointer to pointer
    &node - is pointer to pointer as well
    so
    *ppDest = node; should be enough...

    instead of it you are trying to copy other the pointer the whole p_node structure of bytes (from location that does not contains enogh info) overwriting all memory after ppDest ...

    It does not seems as it should work...
    heh yeah you were right. I really had no idea what I was doing using memcpy. I rewrote it today and just copied the data over and It worked on the first compile. heh.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 01-03-2009, 02:48 PM
  2. Pointers to pointers in linked lists
    By G4B3 in forum C++ Programming
    Replies: 2
    Last Post: 07-23-2008, 03:54 AM
  3. pointers and linked lists in place of arrays
    By kordric in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2008, 10:59 AM
  4. Linked Lists & Pointers
    By fkheng in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 07:26 AM
  5. double linked lists
    By susyb in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2001, 05:09 AM

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