C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-05-2008, 09:59 AM   #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
simo_r is offline   Reply With Quote
Old 05-05-2008, 10:23 AM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
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...
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 05-06-2008, 04:25 AM   #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
simo_r is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
understanding double pointers in functions and linked lists yougene C Programming 9 01-03-2009 03:48 PM
Pointers to pointers in linked lists G4B3 C++ Programming 2 07-23-2008 03:54 AM
pointers and linked lists in place of arrays kordric C++ Programming 8 05-14-2008 10:59 AM
Linked Lists & Pointers fkheng C Programming 4 06-10-2003 07:26 AM
double linked lists susyb C++ Programming 3 11-29-2001 06:09 AM


All times are GMT -6. The time now is 01:36 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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