PDA

View Full Version : Simple Pointer Question


cloke
2002.06.23, 05:54 PM
I have a very simple pointer question. I want to pass two pointers to a function and point the second pointer the the location of the first pointer. This seems like such a simple thing, but for some reason I can't get it to work.

int main( void )
{
NODE *p = new NODE, *b = NULL;
ApplendNode(p, b);

//b should now be the value of p, but it is not.
}

void ApplendNode(NODE *p, NODE *b)
{
b = p;
}

Thanks in advance for any suggestions.

RedWolf
2002.06.23, 08:32 PM
You need to pass the address of the second pointer to the function and modify your assignment:


int main( void )
{
NODE *p = new NODE, *b = NULL;
ApplendNode(p, &b);

//b should now be the value of p, but it is not.
}

void ApplendNode(NODE *p, NODE **b)
{
*b = p;
}

cloke
2002.06.24, 11:51 PM
Thanks for the help.