I am looking at a textbook example of a linked list that implements a stack. I don’t understand why using a pointer to a pointer to the stack is necessary for the push operation. See the following example:
bool push( Element **stack, void *data)
{
Element *elem = new Element;
if(!elem) return false;
elem->data = data;
elem->next = *stack;
*stack = elem;
return true;
}
If anyone can help clarify why the first parameter of the push method is a pointer to a pointer, I would greatly appreciate it. Thanks.
Amazing, thank you for all of the excellent help.
The function needs to modify the value of the Element pointer, so it needs a pointer to that pointer.
Put it another way: a function takes a pointer of something when it needs to modify that thing.
In this case, that something is a pointer itself. So the function ends up taking a pointer to a pointer.