I recently wrote a MyStack class which would work just like STL stack. Now i decided to write MyStack struct, which I will work with using functions instead of methods
struct Stack
{
    int _data;
    Stack* _next;
}*stack;
I have Erase function to delete all the elements and null the first element.
void Erase(Stack* stack)
{
    Stack* cur;
    while (stack != nullptr)
    {
        cur = stack;
        stack = stack->_next;
        delete cur;
    }
}
The problem is that only the local stack pointer is being nulled, not the one I pass pass into the function, so if I call the function again, it will run the loop and trigger a breakpoint. I decided to rewrite the function and after an hour I came up with a solution that actually nulls both local and passed pointers
void Erase(Stack** pStack)
{
    Stack* cur;
    while (*pStack != nullptr)
    {
        cur = *pStack;
        *pStack = (*pStack)->_next;
        delete cur;
    }
}
Why does it work? Why is dereferenced pointer to pointer works differently from just a pointer?
 
    