![]() |
| | #1 |
| Registered User Join Date: May 2008
Posts: 2
| Double pointers and 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;
}
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 | |
| | #2 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| It took me some time to read this horrible code... Code: memcpy(&(*ppDest), &node, sizeof(p_node)); &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 | |
| | #3 | |
| Registered User Join Date: May 2008
Posts: 2
| Quote:
Thanks | |
| simo_r is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |